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

Diff of /sourceforge.net/rdesktop/trunk/rdpsnd.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 492 by stargo, Tue Oct 14 07:46:49 2003 UTC revision 1337 by ossman_, Wed Dec 6 12:31:58 2006 UTC
# Line 1  Line 1 
1  /*  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Sound Channel Process Functions     Sound Channel Process Functions
4     Copyright (C) Matthew Chapman 2003     Copyright (C) Matthew Chapman 2003
# Line 19  Line 19 
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */  */
21    
22    #include <assert.h>
23    
24  #include "rdesktop.h"  #include "rdesktop.h"
25    #include "rdpsnd.h"
26    #include "rdpsnd_dsp.h"
27    
28  #define RDPSND_CLOSE            1  #define RDPSND_CLOSE            1
29  #define RDPSND_WRITE            2  #define RDPSND_WRITE            2
30  #define RDPSND_SET_VOLUME       3  #define RDPSND_SET_VOLUME       3
31  #define RDPSND_UNKNOWN4         4  #define RDPSND_UNKNOWN4         4
32  #define RDPSND_COMPLETION       5  #define RDPSND_COMPLETION       5
33  #define RDPSND_UNKNOWN6         6  #define RDPSND_SERVERTICK       6
34  #define RDPSND_NEGOTIATE        7  #define RDPSND_NEGOTIATE        7
35    
36  #define MAX_FORMATS             10  #define MAX_FORMATS             10
37    #define MAX_QUEUE               10
38    
39    BOOL g_dsp_busy = False;
40    int g_dsp_fd;
41    
42  static VCHANNEL *rdpsnd_channel;  static VCHANNEL *rdpsnd_channel;
43    static struct audio_driver *drivers = NULL;
44    struct audio_driver *current_driver = NULL;
45    
46  static BOOL device_open;  static BOOL device_open;
47  static WAVEFORMATEX formats[MAX_FORMATS];  static WAVEFORMATEX formats[MAX_FORMATS];
48  static unsigned int format_count;  static unsigned int format_count;
49  static unsigned int current_format;  static unsigned int current_format;
50    unsigned int queue_hi, queue_lo, queue_pending;
51    struct audio_packet packet_queue[MAX_QUEUE];
52    
53    void (*wave_out_play) (void);
54    
55  STREAM  static void rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index);
56    static void rdpsnd_queue_init(void);
57    static void rdpsnd_queue_complete_pending(void);
58    static long rdpsnd_queue_next_completion(void);
59    
60    static STREAM
61  rdpsnd_init_packet(uint16 type, uint16 size)  rdpsnd_init_packet(uint16 type, uint16 size)
62  {  {
63          STREAM s;          STREAM s;
64    
65          s = channel_init(rdpsnd_channel, size+4);          s = channel_init(rdpsnd_channel, size + 4);
66          out_uint16_le(s, type);          out_uint16_le(s, type);
67          out_uint16_le(s, size);          out_uint16_le(s, size);
68          return s;          return s;
69  }  }
70    
71  void  static void
72  rdpsnd_send(STREAM s)  rdpsnd_send(STREAM s)
73  {  {
74  #ifdef RDPSND_DEBUG  #ifdef RDPSND_DEBUG
75          printf("RDPSND send:\n");          printf("RDPSND send:\n");
76          hexdump(s->channel_hdr+8, s->end-s->channel_hdr-8);          hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8);
77  #endif  #endif
78    
79          channel_send(s, rdpsnd_channel);          channel_send(s, rdpsnd_channel);
80  }  }
81    
82  void rdpsnd_send_completion(uint16 tick, uint8 packet_index)  static void
83    rdpsnd_send_completion(uint16 tick, uint8 packet_index)
84  {  {
85          STREAM s;          STREAM s;
86    
87          s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);          s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);
88          out_uint16_le(s, tick+50);          out_uint16_le(s, tick);
89          out_uint8(s, packet_index);          out_uint8(s, packet_index);
90          out_uint8(s, 0);          out_uint8(s, 0);
91          s_mark_end(s);          s_mark_end(s);
92          rdpsnd_send(s);          rdpsnd_send(s);
93  }  }
94    
95  void  static void
96  rdpsnd_process_negotiate(STREAM in)  rdpsnd_process_negotiate(STREAM in)
97  {  {
98          unsigned int in_format_count, i;          unsigned int in_format_count, i;
99          WAVEFORMATEX *format;          WAVEFORMATEX *format;
100          STREAM out;          STREAM out;
101          BOOL device_available = False;          BOOL device_available = False;
102            int readcnt;
103            int discardcnt;
104    
105          in_uint8s(in, 14);      /* flags, volume, pitch, UDP port */          in_uint8s(in, 14);      /* flags, volume, pitch, UDP port */
106          in_uint16_le(in, in_format_count);          in_uint16_le(in, in_format_count);
107          in_uint8s(in, 4);       /* pad, status, pad */          in_uint8s(in, 4);       /* pad, status, pad */
108    
109          if (wave_out_open())          if (current_driver->wave_out_open())
110          {          {
111                  wave_out_close();                  current_driver->wave_out_close();
112                  device_available = True;                  device_available = True;
113          }          }
114    
115          format_count = 0;          format_count = 0;
116          if (s_check_rem(in, 18*in_format_count))          if (s_check_rem(in, 18 * in_format_count))
117          {          {
118                  for (i = 0; i < in_format_count; i++)                  for (i = 0; i < in_format_count; i++)
119                  {                  {
# Line 105  rdpsnd_process_negotiate(STREAM in) Line 127  rdpsnd_process_negotiate(STREAM in)
127                          in_uint16_le(in, format->cbSize);                          in_uint16_le(in, format->cbSize);
128    
129                          /* read in the buffer of unknown use */                          /* read in the buffer of unknown use */
130                          int readcnt = format->cbSize;                          readcnt = format->cbSize;
131                          int discardcnt = 0;                          discardcnt = 0;
132                          if (format->cbSize > MAX_CBSIZE)                          if (format->cbSize > MAX_CBSIZE)
133                          {                          {
134                                  fprintf(stderr, "cbSize too large for buffer: %d\n", format->cbSize);                                  fprintf(stderr, "cbSize too large for buffer: %d\n",
135                                            format->cbSize);
136                                  readcnt = MAX_CBSIZE;                                  readcnt = MAX_CBSIZE;
137                                  discardcnt = format->cbSize - MAX_CBSIZE;                                  discardcnt = format->cbSize - MAX_CBSIZE;
138                          }                          }
139                          in_uint8a(in, format->cb, readcnt);                          in_uint8a(in, format->cb, readcnt);
140                          in_uint8s(in, discardcnt);                          in_uint8s(in, discardcnt);
141    
142                          if (device_available && wave_out_format_supported(format))                          if (device_available && current_driver->wave_out_format_supported(format))
143                          {                          {
144                                  format_count++;                                  format_count++;
145                                  if (format_count == MAX_FORMATS)                                  if (format_count == MAX_FORMATS)
# Line 125  rdpsnd_process_negotiate(STREAM in) Line 148  rdpsnd_process_negotiate(STREAM in)
148                  }                  }
149          }          }
150    
151          out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18*format_count);          out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18 * format_count);
152          out_uint32_le(out, 3);          /* flags */          out_uint32_le(out, 3);  /* flags */
153          out_uint32(out, 0xffffffff);    /* volume */          out_uint32(out, 0xffffffff);    /* volume */
154          out_uint32(out, 0);             /* pitch */          out_uint32(out, 0);     /* pitch */
155          out_uint16(out, 0);             /* UDP port */          out_uint16(out, 0);     /* UDP port */
156    
157          out_uint16_le(out, format_count);          out_uint16_le(out, format_count);
158          out_uint8(out, 0x95);           /* pad? */          out_uint8(out, 0x95);   /* pad? */
159          out_uint16_le(out, 2);          /* status */          out_uint16_le(out, 2);  /* status */
160          out_uint8(out, 0x77);           /* pad? */          out_uint8(out, 0x77);   /* pad? */
161    
162          for (i = 0; i < format_count; i++)          for (i = 0; i < format_count; i++)
163          {          {
# Line 145  rdpsnd_process_negotiate(STREAM in) Line 168  rdpsnd_process_negotiate(STREAM in)
168                  out_uint32_le(out, format->nAvgBytesPerSec);                  out_uint32_le(out, format->nAvgBytesPerSec);
169                  out_uint16_le(out, format->nBlockAlign);                  out_uint16_le(out, format->nBlockAlign);
170                  out_uint16_le(out, format->wBitsPerSample);                  out_uint16_le(out, format->wBitsPerSample);
171                  out_uint16(out, 0); /* cbSize */                  out_uint16(out, 0);     /* cbSize */
172          }          }
173    
174          s_mark_end(out);          s_mark_end(out);
175          rdpsnd_send(out);          rdpsnd_send(out);
176  }  }
177    
178  void  static void
179  rdpsnd_process_unknown6(STREAM in)  rdpsnd_process_servertick(STREAM in)
180  {  {
181          uint16 unknown1, unknown2;          uint16 tick1, tick2;
182          STREAM out;          STREAM out;
183    
184          /* in_uint8s(in, 4); unknown */          /* in_uint8s(in, 4); unknown */
185          in_uint16_le(in, unknown1);          in_uint16_le(in, tick1);
186          in_uint16_le(in, unknown2);          in_uint16_le(in, tick2);
187    
188          out = rdpsnd_init_packet(RDPSND_UNKNOWN6 | 0x2300, 4);          out = rdpsnd_init_packet(RDPSND_SERVERTICK | 0x2300, 4);
189          out_uint16_le(out, unknown1);          out_uint16_le(out, tick1);
190          out_uint16_le(out, unknown2);          out_uint16_le(out, tick2);
191          s_mark_end(out);          s_mark_end(out);
192          rdpsnd_send(out);          rdpsnd_send(out);
193  }  }
194    
195  void  static void
196  rdpsnd_process(STREAM s)  rdpsnd_process(STREAM s)
197  {  {
198          uint8 type;          uint8 type;
# Line 178  rdpsnd_process(STREAM s) Line 201  rdpsnd_process(STREAM s)
201          static uint16 tick, format;          static uint16 tick, format;
202          static uint8 packet_index;          static uint8 packet_index;
203          static BOOL awaiting_data_packet;          static BOOL awaiting_data_packet;
204            static unsigned char missing_bytes[4] = { 0, 0, 0, 0 };
205    
206  #ifdef RDPSND_DEBUG  #ifdef RDPSND_DEBUG
207          printf("RDPSND recv:\n");          printf("RDPSND recv:\n");
208          hexdump(s->p, s->end-s->p);          hexdump(s->p, s->end - s->p);
209  #endif  #endif
210    
211          if (awaiting_data_packet)          if (awaiting_data_packet)
# Line 194  rdpsnd_process(STREAM s) Line 218  rdpsnd_process(STREAM s)
218    
219                  if (!device_open || (format != current_format))                  if (!device_open || (format != current_format))
220                  {                  {
221                          if (!device_open && !wave_out_open())                          if (!device_open && !current_driver->wave_out_open())
222                          {                          {
223                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
224                                  return;                                  return;
225                          }                          }
226                          if (!wave_out_set_format(&formats[format]))                          if (!current_driver->wave_out_set_format(&formats[format]))
227                          {                          {
228                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
229                                  wave_out_close();                                  current_driver->wave_out_close();
230                                  device_open = False;                                  device_open = False;
231                                  return;                                  return;
232                          }                          }
# Line 210  rdpsnd_process(STREAM s) Line 234  rdpsnd_process(STREAM s)
234                          current_format = format;                          current_format = format;
235                  }                  }
236    
237                  wave_out_write(s, tick, packet_index);                  /* Insert the 4 missing bytes retrieved from last RDPSND_WRITE */
238                    memcpy(s->data, missing_bytes, 4);
239    
240                    rdpsnd_queue_write(rdpsnd_dsp_process
241                                       (s, current_driver, &formats[current_format]), tick,
242                                       packet_index);
243                  awaiting_data_packet = False;                  awaiting_data_packet = False;
244                  return;                  return;
245          }          }
246    
247          in_uint8(s, type);          in_uint8(s, type);
248          in_uint8s(s, 1); /* unknown? */          in_uint8s(s, 1);        /* unknown? */
249          in_uint16_le(s, datalen);          in_uint16_le(s, datalen);
250    
251          switch (type)          switch (type)
252          {          {
253          case RDPSND_WRITE:                  case RDPSND_WRITE:
254                  in_uint16_le(s, tick);                          in_uint16_le(s, tick);
255                  in_uint16_le(s, format);                          in_uint16_le(s, format);
256                  in_uint8(s, packet_index);                          in_uint8(s, packet_index);
257                  awaiting_data_packet = True;                          /* Here are our lost bytes, but why? */
258                  break;                          memcpy(missing_bytes, s->end - 4, 4);
259          case RDPSND_CLOSE:                          awaiting_data_packet = True;
260                  wave_out_close();                          break;
261                  device_open = False;                  case RDPSND_CLOSE:
262                  break;                          current_driver->wave_out_close();
263          case RDPSND_NEGOTIATE:                          device_open = False;
264                  rdpsnd_process_negotiate(s);                          break;
265                  break;                  case RDPSND_NEGOTIATE:
266          case RDPSND_UNKNOWN6:                          rdpsnd_process_negotiate(s);
267                  rdpsnd_process_unknown6(s);                          break;
268                  break;                  case RDPSND_SERVERTICK:
269          case RDPSND_SET_VOLUME:                          rdpsnd_process_servertick(s);
270                  in_uint32(s, volume);                          break;
271                  if ( device_open )                  case RDPSND_SET_VOLUME:
272                            in_uint32(s, volume);
273                            if (device_open)
274                            {
275                                    current_driver->wave_out_volume((volume & 0xffff),
276                                                                    (volume & 0xffff0000) >> 16);
277                            }
278                            break;
279                    default:
280                            unimpl("RDPSND packet type %d\n", type);
281                            break;
282            }
283    }
284    
285    static BOOL
286    rdpsnd_auto_open(void)
287    {
288            static BOOL failed = False;
289    
290            if (!failed)
291            {
292                    struct audio_driver *auto_driver = current_driver;
293    
294                    current_driver = drivers;
295                    while (current_driver != NULL)
296                  {                  {
297                          wave_out_volume((volume & 0xffff), (volume & 0xffff0000) >> 16);                          DEBUG(("trying %s...\n", current_driver->name));
298                            if (current_driver->wave_out_open())
299                            {
300                                    DEBUG(("selected %s\n", current_driver->name));
301                                    return True;
302                            }
303                            g_dsp_fd = 0;
304                            current_driver = current_driver->next;
305                  }                  }
306                  break;  
307          default:                  warning("no working audio-driver found\n");
308                  unimpl("RDPSND packet type %d\n", type);                  failed = True;
309                  break;                  current_driver = auto_driver;
310          }          }
311    
312            return False;
313    }
314    
315    static void
316    rdpsnd_register_drivers(char *options)
317    {
318            struct audio_driver **reg;
319    
320            /* The order of registrations define the probe-order
321               when opening the device for the first time */
322            reg = &drivers;
323    #if defined(RDPSND_ALSA)
324            *reg = alsa_register(options);
325            assert(*reg);
326            reg = &((*reg)->next);
327    #endif
328    #if defined(RDPSND_SUN)
329            *reg = sun_register(options);
330            assert(*reg);
331            reg = &((*reg)->next);
332    #endif
333    #if defined(RDPSND_OSS)
334            *reg = oss_register(options);
335            assert(*reg);
336            reg = &((*reg)->next);
337    #endif
338    #if defined(RDPSND_SGI)
339            *reg = sgi_register(options);
340            assert(*reg);
341            reg = &((*reg)->next);
342    #endif
343    #if defined(RDPSND_LIBAO)
344            *reg = libao_register(options);
345            assert(*reg);
346            reg = &((*reg)->next);
347    #endif
348            *reg = NULL;
349  }  }
350    
351  BOOL  BOOL
352  rdpsnd_init(void)  rdpsnd_init(char *optarg)
353  {  {
354            static struct audio_driver auto_driver;
355            struct audio_driver *pos;
356            char *driver = NULL, *options = NULL;
357    
358            drivers = NULL;
359    
360          rdpsnd_channel =          rdpsnd_channel =
361                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
362                                   rdpsnd_process);                                   rdpsnd_process);
363          return (rdpsnd_channel != NULL);  
364            if (rdpsnd_channel == NULL)
365            {
366                    error("channel_register\n");
367                    return False;
368            }
369    
370            rdpsnd_queue_init();
371    
372            if (optarg != NULL && strlen(optarg) > 0)
373            {
374                    driver = options = optarg;
375    
376                    while (*options != '\0' && *options != ':')
377                            options++;
378    
379                    if (*options == ':')
380                    {
381                            *options = '\0';
382                            options++;
383                    }
384    
385                    if (*options == '\0')
386                            options = NULL;
387            }
388    
389            rdpsnd_register_drivers(options);
390    
391            if (!driver)
392            {
393                    auto_driver.wave_out_open = &rdpsnd_auto_open;
394                    current_driver = &auto_driver;
395                    return True;
396            }
397    
398            pos = drivers;
399            while (pos != NULL)
400            {
401                    if (!strcmp(pos->name, driver))
402                    {
403                            DEBUG(("selected %s\n", pos->name));
404                            current_driver = pos;
405                            return True;
406                    }
407                    pos = pos->next;
408            }
409            return False;
410    }
411    
412    void
413    rdpsnd_show_help(void)
414    {
415            struct audio_driver *pos;
416    
417            rdpsnd_register_drivers(NULL);
418    
419            pos = drivers;
420            while (pos != NULL)
421            {
422                    fprintf(stderr, "                     %s:\t%s\n", pos->name, pos->description);
423                    pos = pos->next;
424            }
425    }
426    
427    void
428    rdpsnd_play(void)
429    {
430            current_driver->wave_out_play();
431    }
432    
433    void
434    rdpsnd_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
435    {
436            long next_pending;
437    
438            if (g_dsp_busy)
439            {
440                    FD_SET(g_dsp_fd, wfds);
441                    *n = (g_dsp_fd > *n) ? g_dsp_fd : *n;
442            }
443    
444            next_pending = rdpsnd_queue_next_completion();
445            if (next_pending >= 0)
446            {
447                    long cur_timeout;
448    
449                    cur_timeout = tv->tv_sec * 1000000 + tv->tv_usec;
450                    if (cur_timeout > next_pending)
451                    {
452                            tv->tv_sec = next_pending / 1000000;
453                            tv->tv_usec = next_pending % 1000000;
454                    }
455            }
456    }
457    
458    void
459    rdpsnd_check_fds(fd_set * rfds, fd_set * wfds)
460    {
461            rdpsnd_queue_complete_pending();
462    
463            if (g_dsp_busy && FD_ISSET(g_dsp_fd, wfds))
464                    rdpsnd_play();
465    }
466    
467    static void
468    rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index)
469    {
470            struct audio_packet *packet = &packet_queue[queue_hi];
471            unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
472    
473            if (next_hi == queue_pending)
474            {
475                    error("No space to queue audio packet\n");
476                    return;
477            }
478    
479            queue_hi = next_hi;
480    
481            packet->s = *s;
482            packet->tick = tick;
483            packet->index = index;
484    
485            gettimeofday(&packet->arrive_tv, NULL);
486    
487            if (!g_dsp_busy)
488                    current_driver->wave_out_play();
489    }
490    
491    struct audio_packet *
492    rdpsnd_queue_current_packet(void)
493    {
494            return &packet_queue[queue_lo];
495    }
496    
497    BOOL
498    rdpsnd_queue_empty(void)
499    {
500            return (queue_lo == queue_hi);
501    }
502    
503    static void
504    rdpsnd_queue_init(void)
505    {
506            queue_pending = queue_lo = queue_hi = 0;
507    }
508    
509    void
510    rdpsnd_queue_next(unsigned long completed_in_us)
511    {
512            struct audio_packet *packet;
513    
514            assert(!rdpsnd_queue_empty());
515    
516            packet = &packet_queue[queue_lo];
517    
518            gettimeofday(&packet->completion_tv, NULL);
519    
520            packet->completion_tv.tv_usec += completed_in_us;
521            packet->completion_tv.tv_sec += packet->completion_tv.tv_usec / 1000000;
522            packet->completion_tv.tv_usec %= 1000000;
523    
524            queue_lo = (queue_lo + 1) % MAX_QUEUE;
525    
526            rdpsnd_queue_complete_pending();
527    }
528    
529    int
530    rdpsnd_queue_next_tick(void)
531    {
532            if (((queue_lo + 1) % MAX_QUEUE) != queue_hi)
533            {
534                    return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick;
535            }
536            else
537            {
538                    return (packet_queue[queue_lo].tick + 65535) % 65536;
539            }
540    }
541    
542    static void
543    rdpsnd_queue_complete_pending(void)
544    {
545            struct timeval now;
546            long elapsed;
547            struct audio_packet *packet;
548    
549            gettimeofday(&now, NULL);
550    
551            while (queue_pending != queue_lo)
552            {
553                    packet = &packet_queue[queue_pending];
554    
555                    if (now.tv_sec < packet->completion_tv.tv_sec)
556                            break;
557    
558                    if ((now.tv_sec == packet->completion_tv.tv_sec) &&
559                        (now.tv_usec < packet->completion_tv.tv_usec))
560                            break;
561    
562                    elapsed = (packet->completion_tv.tv_sec - packet->arrive_tv.tv_sec) * 1000000 +
563                            (packet->completion_tv.tv_usec - packet->arrive_tv.tv_usec);
564                    elapsed /= 1000;
565    
566                    xfree(packet->s.data);
567                    rdpsnd_send_completion((packet->tick + elapsed) % 65536, packet->index);
568                    queue_pending = (queue_pending + 1) % MAX_QUEUE;
569            }
570    }
571    
572    static long
573    rdpsnd_queue_next_completion(void)
574    {
575            struct audio_packet *packet;
576            long remaining;
577            struct timeval now;
578    
579            if (queue_pending == queue_lo)
580                    return -1;
581    
582            gettimeofday(&now, NULL);
583    
584            packet = &packet_queue[queue_pending];
585    
586            remaining = (packet->completion_tv.tv_sec - now.tv_sec) * 1000000 +
587                    (packet->completion_tv.tv_usec - now.tv_usec);
588    
589            if (remaining < 0)
590                    return 0;
591    
592            return remaining;
593  }  }

Legend:
Removed from v.492  
changed lines
  Added in v.1337

  ViewVC Help
Powered by ViewVC 1.1.26