/[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 1332 by stargo, Mon Nov 6 23:05:39 2006 UTC revision 1350 by stargo, Sun Dec 24 13:48:50 2006 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- 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 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
5     Copyright (C) Matthew Chapman 2003     Copyright (C) Matthew Chapman 2003
6     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
7    
# Line 30  Line 31 
31  #define RDPSND_SET_VOLUME       3  #define RDPSND_SET_VOLUME       3
32  #define RDPSND_UNKNOWN4         4  #define RDPSND_UNKNOWN4         4
33  #define RDPSND_COMPLETION       5  #define RDPSND_COMPLETION       5
34  #define RDPSND_SERVERTICK       6  #define RDPSND_PING                     6
35  #define RDPSND_NEGOTIATE        7  #define RDPSND_NEGOTIATE        7
36    
37  #define MAX_FORMATS             10  #define MAX_FORMATS             10
38  #define MAX_QUEUE               10  #define MAX_QUEUE               10
39    
 BOOL g_dsp_busy = False;  
 int g_dsp_fd;  
   
40  static VCHANNEL *rdpsnd_channel;  static VCHANNEL *rdpsnd_channel;
41  static struct audio_driver *drivers = NULL;  static struct audio_driver *drivers = NULL;
42  struct audio_driver *current_driver = NULL;  struct audio_driver *current_driver = NULL;
# Line 50  static unsigned int current_format; Line 48  static unsigned int current_format;
48  unsigned int queue_hi, queue_lo, queue_pending;  unsigned int queue_hi, queue_lo, queue_pending;
49  struct audio_packet packet_queue[MAX_QUEUE];  struct audio_packet packet_queue[MAX_QUEUE];
50    
51    static uint8 packet_opcode;
52    static struct stream packet;
53    
54  void (*wave_out_play) (void);  void (*wave_out_play) (void);
55    
56  static void rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index);  static void rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index);
# Line 71  rdpsnd_init_packet(uint16 type, uint16 s Line 72  rdpsnd_init_packet(uint16 type, uint16 s
72  static void  static void
73  rdpsnd_send(STREAM s)  rdpsnd_send(STREAM s)
74  {  {
 #ifdef RDPSND_DEBUG  
         printf("RDPSND send:\n");  
         hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8);  
 #endif  
   
75          channel_send(s, rdpsnd_channel);          channel_send(s, rdpsnd_channel);
76  }  }
77    
# Line 90  rdpsnd_send_completion(uint16 tick, uint Line 86  rdpsnd_send_completion(uint16 tick, uint
86          out_uint8(s, 0);          out_uint8(s, 0);
87          s_mark_end(s);          s_mark_end(s);
88          rdpsnd_send(s);          rdpsnd_send(s);
89    
90            DEBUG_SOUND(("RDPSND: -> RDPSND_COMPLETION(tick: %u, index: %u)\n",
91                         (unsigned) tick, (unsigned) packet_index));
92    }
93    
94    
95    static BOOL
96    rdpsnd_auto_select(void)
97    {
98            static BOOL failed = False;
99    
100            if (!failed)
101            {
102                    current_driver = drivers;
103                    while (current_driver != NULL)
104                    {
105                            DEBUG(("trying %s...\n", current_driver->name));
106                            if (current_driver->wave_out_open())
107                            {
108                                    DEBUG(("selected %s\n", current_driver->name));
109                                    current_driver->wave_out_close();
110                                    return True;
111                            }
112                            current_driver = current_driver->next;
113                    }
114    
115                    warning("no working audio-driver found\n");
116                    failed = True;
117                    current_driver = NULL;
118            }
119    
120            return False;
121  }  }
122    
123  static void  static void
124  rdpsnd_process_negotiate(STREAM in)  rdpsnd_process_negotiate(STREAM in)
125  {  {
126          unsigned int in_format_count, i;          uint16 in_format_count, i;
127            uint8 pad;
128            uint16 version;
129          WAVEFORMATEX *format;          WAVEFORMATEX *format;
130          STREAM out;          STREAM out;
131          BOOL device_available = False;          BOOL device_available = False;
132          int readcnt;          int readcnt;
133          int discardcnt;          int discardcnt;
134    
135          in_uint8s(in, 14);      /* flags, volume, pitch, UDP port */          in_uint8s(in, 14);      /* initial bytes not valid from server */
136          in_uint16_le(in, in_format_count);          in_uint16_le(in, in_format_count);
137          in_uint8s(in, 4);       /* pad, status, pad */          in_uint8(in, pad);
138            in_uint16_le(in, version);
139            in_uint8s(in, 1);       /* padding */
140    
141          if (current_driver->wave_out_open())          DEBUG_SOUND(("RDPSND: RDPSND_NEGOTIATE(formats: %d, pad: 0x%02x, version: %x)\n",
142                         (int) in_format_count, (unsigned) pad, (unsigned) version));
143    
144            if (!current_driver)
145                    rdpsnd_auto_select();
146    
147            if (current_driver)
148          {          {
                 current_driver->wave_out_close();  
149                  device_available = True;                  device_available = True;
150          }          }
151    
# Line 155  rdpsnd_process_negotiate(STREAM in) Line 192  rdpsnd_process_negotiate(STREAM in)
192          out_uint16(out, 0);     /* UDP port */          out_uint16(out, 0);     /* UDP port */
193    
194          out_uint16_le(out, format_count);          out_uint16_le(out, format_count);
195          out_uint8(out, 0x95);   /* pad? */          out_uint8(out, 0);      /* padding */
196          out_uint16_le(out, 2);  /* status */          out_uint16_le(out, 2);  /* version */
197          out_uint8(out, 0x77);   /* pad? */          out_uint8(out, 0);      /* padding */
198    
199          for (i = 0; i < format_count; i++)          for (i = 0; i < format_count; i++)
200          {          {
# Line 172  rdpsnd_process_negotiate(STREAM in) Line 209  rdpsnd_process_negotiate(STREAM in)
209          }          }
210    
211          s_mark_end(out);          s_mark_end(out);
212    
213            DEBUG_SOUND(("RDPSND: -> RDPSND_NEGOTIATE(formats: %d)\n", (int) format_count));
214    
215          rdpsnd_send(out);          rdpsnd_send(out);
216  }  }
217    
218  static void  static void
219  rdpsnd_process_servertick(STREAM in)  rdpsnd_process_ping(STREAM in)
220  {  {
221          uint16 tick1, tick2;          uint16 tick;
222          STREAM out;          STREAM out;
223    
224          /* in_uint8s(in, 4); unknown */          in_uint16_le(in, tick);
225          in_uint16_le(in, tick1);  
226          in_uint16_le(in, tick2);          DEBUG_SOUND(("RDPSND: RDPSND_PING(tick: 0x%04x)\n", (unsigned) tick));
227    
228          out = rdpsnd_init_packet(RDPSND_SERVERTICK | 0x2300, 4);          out = rdpsnd_init_packet(RDPSND_PING | 0x2300, 4);
229          out_uint16_le(out, tick1);          out_uint16_le(out, tick);
230          out_uint16_le(out, tick2);          out_uint16_le(out, 0);
231          s_mark_end(out);          s_mark_end(out);
232          rdpsnd_send(out);          rdpsnd_send(out);
233    
234            DEBUG_SOUND(("RDPSND: -> (tick: 0x%04x)\n", (unsigned) tick));
235  }  }
236    
237  static void  static void
238  rdpsnd_process(STREAM s)  rdpsnd_process_packet(uint8 opcode, STREAM s)
239  {  {
240          uint8 type;          uint16 vol_left, vol_right;
         uint16 datalen;  
         uint32 volume;  
241          static uint16 tick, format;          static uint16 tick, format;
242          static uint8 packet_index;          static uint8 packet_index;
         static BOOL awaiting_data_packet;  
         static unsigned char missing_bytes[4] = { 0, 0, 0, 0 };  
   
 #ifdef RDPSND_DEBUG  
         printf("RDPSND recv:\n");  
         hexdump(s->p, s->end - s->p);  
 #endif  
243    
244          if (awaiting_data_packet)          switch (opcode)
245          {          {
246                  if (format >= MAX_FORMATS)                  case RDPSND_WRITE:
247                  {                          in_uint16_le(s, tick);
248                          error("RDPSND: Invalid format index\n");                          in_uint16_le(s, format);
249                          return;                          in_uint8(s, packet_index);
250                  }                          in_uint8s(s, 3);
251                            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));
252    
253                  if (!device_open || (format != current_format))                          if (format >= MAX_FORMATS)
                 {  
                         if (!device_open && !current_driver->wave_out_open())  
254                          {                          {
255                                  rdpsnd_send_completion(tick, packet_index);                                  error("RDPSND: Invalid format index\n");
256                                  return;                                  break;
257                          }                          }
258                          if (!current_driver->wave_out_set_format(&formats[format]))  
259                            if (!device_open || (format != current_format))
260                          {                          {
261                                  rdpsnd_send_completion(tick, packet_index);                                  /*
262                                  current_driver->wave_out_close();                                   * If we haven't selected a device by now, then either
263                                  device_open = False;                                   * we've failed to find a working device, or the server
264                                  return;                                   * is sending bogus RDPSND_WRITE.
265                                     */
266                                    if (!current_driver)
267                                    {
268                                            rdpsnd_send_completion(tick, packet_index);
269                                            break;
270                                    }
271                                    if (!device_open && !current_driver->wave_out_open())
272                                    {
273                                            rdpsnd_send_completion(tick, packet_index);
274                                            break;
275                                    }
276                                    if (!current_driver->wave_out_set_format(&formats[format]))
277                                    {
278                                            rdpsnd_send_completion(tick, packet_index);
279                                            current_driver->wave_out_close();
280                                            device_open = False;
281                                            break;
282                                    }
283                                    device_open = True;
284                                    current_format = format;
285                          }                          }
                         device_open = True;  
                         current_format = format;  
                 }  
   
                 /* Insert the 4 missing bytes retrieved from last RDPSND_WRITE */  
                 memcpy(s->data, missing_bytes, 4);  
   
                 rdpsnd_queue_write(rdpsnd_dsp_process  
                                    (s, current_driver, &formats[current_format]), tick,  
                                    packet_index);  
                 awaiting_data_packet = False;  
                 return;  
         }  
286    
287          in_uint8(s, type);                          rdpsnd_queue_write(rdpsnd_dsp_process
288          in_uint8s(s, 1);        /* unknown? */                                             (s->p, s->end - s->p, current_driver,
289          in_uint16_le(s, datalen);                                              &formats[current_format]), tick, packet_index);
290                            return;
         switch (type)  
         {  
                 case RDPSND_WRITE:  
                         in_uint16_le(s, tick);  
                         in_uint16_le(s, format);  
                         in_uint8(s, packet_index);  
                         /* Here are our lost bytes, but why? */  
                         memcpy(missing_bytes, s->end - 4, 4);  
                         awaiting_data_packet = True;  
291                          break;                          break;
292                  case RDPSND_CLOSE:                  case RDPSND_CLOSE:
293                          current_driver->wave_out_close();                          DEBUG_SOUND(("RDPSND: RDPSND_CLOSE()\n"));
294                            if (device_open)
295                                    current_driver->wave_out_close();
296                          device_open = False;                          device_open = False;
297                          break;                          break;
298                  case RDPSND_NEGOTIATE:                  case RDPSND_NEGOTIATE:
299                          rdpsnd_process_negotiate(s);                          rdpsnd_process_negotiate(s);
300                          break;                          break;
301                  case RDPSND_SERVERTICK:                  case RDPSND_PING:
302                          rdpsnd_process_servertick(s);                          rdpsnd_process_ping(s);
303                          break;                          break;
304                  case RDPSND_SET_VOLUME:                  case RDPSND_SET_VOLUME:
305                          in_uint32(s, volume);                          in_uint16_le(s, vol_left);
306                            in_uint16_le(s, vol_right);
307                            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));
308                          if (device_open)                          if (device_open)
309                          {                                  current_driver->wave_out_volume(vol_left, vol_right);
                                 current_driver->wave_out_volume((volume & 0xffff),  
                                                                 (volume & 0xffff0000) >> 16);  
                         }  
310                          break;                          break;
311                  default:                  default:
312                          unimpl("RDPSND packet type %d\n", type);                          unimpl("RDPSND packet type %x\n", opcode);
313                          break;                          break;
314          }          }
315  }  }
316    
317  static BOOL  static void
318  rdpsnd_auto_open(void)  rdpsnd_process(STREAM s)
319  {  {
320          static BOOL failed = False;          uint16 len;
321    
322          if (!failed)          while (!s_check_end(s))
323          {          {
324                  struct audio_driver *auto_driver = current_driver;                  /* New packet */
325                    if (packet.size == 0)
                 current_driver = drivers;  
                 while (current_driver != NULL)  
326                  {                  {
327                          DEBUG(("trying %s...\n", current_driver->name));                          if ((s->end - s->p) < 4)
                         if (current_driver->wave_out_open())  
328                          {                          {
329                                  DEBUG(("selected %s\n", current_driver->name));                                  error("RDPSND: Split at packet header. Things will go south from here...\n");
330                                  return True;                                  return;
331                          }                          }
332                          g_dsp_fd = 0;                          in_uint8(s, packet_opcode);
333                          current_driver = current_driver->next;                          in_uint8s(s, 1);        /* Padding */
334                            in_uint16_le(s, len);
335    
336                            DEBUG_SOUND(("RDPSND: == Opcode %x Length: %d ==\n",
337                                         (int) packet_opcode, (int) len));
338    
339                            packet.p = packet.data;
340                            packet.end = packet.data + len;
341                            packet.size = len;
342                  }                  }
343                    else
344                    {
345                            len = MIN(s->end - s->p, packet.end - packet.p);
346    
347                  warning("no working audio-driver found\n");                          /* Microsoft's server is so broken it's not even funny... */
348                  failed = True;                          if (packet_opcode == RDPSND_WRITE)
349                  current_driver = auto_driver;                          {
350          }                                  if ((packet.p - packet.data) < 12)
351                                            len = MIN(len, 12 - (packet.p - packet.data));
352                                    else if ((packet.p - packet.data) == 12)
353                                    {
354                                            DEBUG_SOUND(("RDPSND: Eating 4 bytes of %d bytes...\n",
355                                                         len));
356                                            in_uint8s(s, 4);
357                                            len -= 4;
358                                    }
359                            }
360    
361          return False;                          in_uint8a(s, packet.p, len);
362                            packet.p += len;
363                    }
364    
365                    /* Packet fully assembled */
366                    if (packet.p == packet.end)
367                    {
368                            packet.p = packet.data;
369                            rdpsnd_process_packet(packet_opcode, &packet);
370                            packet.size = 0;
371                    }
372            }
373  }  }
374    
375  static void  static void
# Line 351  rdpsnd_register_drivers(char *options) Line 411  rdpsnd_register_drivers(char *options)
411  BOOL  BOOL
412  rdpsnd_init(char *optarg)  rdpsnd_init(char *optarg)
413  {  {
         static struct audio_driver auto_driver;  
414          struct audio_driver *pos;          struct audio_driver *pos;
415          char *driver = NULL, *options = NULL;          char *driver = NULL, *options = NULL;
416    
417          drivers = NULL;          drivers = NULL;
418    
419            packet.data = xmalloc(65536);
420            packet.p = packet.end = packet.data;
421            packet.size = 0;
422    
423          rdpsnd_channel =          rdpsnd_channel =
424                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
425                                   rdpsnd_process);                                   rdpsnd_process);
# Line 389  rdpsnd_init(char *optarg) Line 452  rdpsnd_init(char *optarg)
452          rdpsnd_register_drivers(options);          rdpsnd_register_drivers(options);
453    
454          if (!driver)          if (!driver)
         {  
                 auto_driver.wave_out_open = &rdpsnd_auto_open;  
                 current_driver = &auto_driver;  
455                  return True;                  return True;
         }  
456    
457          pos = drivers;          pos = drivers;
458          while (pos != NULL)          while (pos != NULL)
# Line 425  rdpsnd_show_help(void) Line 484  rdpsnd_show_help(void)
484  }  }
485    
486  void  void
 rdpsnd_play(void)  
 {  
         current_driver->wave_out_play();  
 }  
   
 void  
487  rdpsnd_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)  rdpsnd_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
488  {  {
489          long next_pending;          long next_pending;
490    
491          if (g_dsp_busy)          if (device_open)
492          {                  current_driver->add_fds(n, rfds, wfds, tv);
                 FD_SET(g_dsp_fd, wfds);  
                 *n = (g_dsp_fd > *n) ? g_dsp_fd : *n;  
         }  
493    
494          next_pending = rdpsnd_queue_next_completion();          next_pending = rdpsnd_queue_next_completion();
495          if (next_pending >= 0)          if (next_pending >= 0)
# Line 460  rdpsnd_check_fds(fd_set * rfds, fd_set * Line 510  rdpsnd_check_fds(fd_set * rfds, fd_set *
510  {  {
511          rdpsnd_queue_complete_pending();          rdpsnd_queue_complete_pending();
512    
513          if (g_dsp_busy && FD_ISSET(g_dsp_fd, wfds))          if (device_open)
514                  rdpsnd_play();                  current_driver->check_fds(rfds, wfds);
515  }  }
516    
517  static void  static void
# Line 483  rdpsnd_queue_write(STREAM s, uint16 tick Line 533  rdpsnd_queue_write(STREAM s, uint16 tick
533          packet->index = index;          packet->index = index;
534    
535          gettimeofday(&packet->arrive_tv, NULL);          gettimeofday(&packet->arrive_tv, NULL);
   
         if (!g_dsp_busy)  
                 current_driver->wave_out_play();  
536  }  }
537    
538  struct audio_packet *  struct audio_packet *
# Line 561  rdpsnd_queue_complete_pending(void) Line 608  rdpsnd_queue_complete_pending(void)
608    
609                  elapsed = (packet->completion_tv.tv_sec - packet->arrive_tv.tv_sec) * 1000000 +                  elapsed = (packet->completion_tv.tv_sec - packet->arrive_tv.tv_sec) * 1000000 +
610                          (packet->completion_tv.tv_usec - packet->arrive_tv.tv_usec);                          (packet->completion_tv.tv_usec - packet->arrive_tv.tv_usec);
611                    elapsed /= 1000;
612    
613                  xfree(packet->s.data);                  xfree(packet->s.data);
614                  rdpsnd_send_completion((packet->tick + elapsed) % 65536, packet->index);                  rdpsnd_send_completion((packet->tick + elapsed) % 65536, packet->index);

Legend:
Removed from v.1332  
changed lines
  Added in v.1350

  ViewVC Help
Powered by ViewVC 1.1.26