/[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 474 by matthewc, Tue Sep 30 09:11:08 2003 UTC revision 1264 by stargo, Sun Sep 17 19:08:14 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 20  Line 20 
20  */  */
21    
22  #include "rdesktop.h"  #include "rdesktop.h"
23    #include "rdpsnd.h"
24    #include "rdpsnd_dsp.h"
25    
26  #define RDPSND_CLOSE            1  #define RDPSND_CLOSE            1
27  #define RDPSND_WRITE            2  #define RDPSND_WRITE            2
28  #define RDPSND_SET_VOLUME       3  #define RDPSND_SET_VOLUME       3
29  #define RDPSND_UNKNOWN4         4  #define RDPSND_UNKNOWN4         4
30  #define RDPSND_COMPLETION       5  #define RDPSND_COMPLETION       5
31  #define RDPSND_UNKNOWN6         6  #define RDPSND_SERVERTICK       6
32  #define RDPSND_NEGOTIATE        7  #define RDPSND_NEGOTIATE        7
33    
34  #define MAX_FORMATS             10  #define MAX_FORMATS             10
35    #define MAX_QUEUE               10
36    
37    BOOL g_dsp_busy = False;
38    int g_dsp_fd;
39    
40  static VCHANNEL *rdpsnd_channel;  static VCHANNEL *rdpsnd_channel;
41    static struct audio_driver *drivers = NULL;
42    struct audio_driver *current_driver = NULL;
43    
44  static BOOL device_open;  static BOOL device_open;
45  static WAVEFORMATEX formats[MAX_FORMATS];  static WAVEFORMATEX formats[MAX_FORMATS];
46  static unsigned int format_count;  static unsigned int format_count;
47  static unsigned int current_format;  static unsigned int current_format;
48    unsigned int queue_hi, queue_lo;
49    struct audio_packet packet_queue[MAX_QUEUE];
50    
51    void (*wave_out_play) (void);
52    
53  STREAM  static STREAM
54  rdpsnd_init_packet(uint16 type, uint16 size)  rdpsnd_init_packet(uint16 type, uint16 size)
55  {  {
56          STREAM s;          STREAM s;
57    
58          s = channel_init(rdpsnd_channel, size+4);          s = channel_init(rdpsnd_channel, size + 4);
59          out_uint16_le(s, type);          out_uint16_le(s, type);
60          out_uint16_le(s, size);          out_uint16_le(s, size);
61          return s;          return s;
62  }  }
63    
64  void  static void
65  rdpsnd_send(STREAM s)  rdpsnd_send(STREAM s)
66  {  {
67  #ifdef RDPSND_DEBUG  #ifdef RDPSND_DEBUG
68          printf("RDPSND send:\n");          printf("RDPSND send:\n");
69          hexdump(s->channel_hdr+8, s->end-s->channel_hdr-8);          hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8);
70  #endif  #endif
71    
72          channel_send(s, rdpsnd_channel);          channel_send(s, rdpsnd_channel);
73  }  }
74    
75  void rdpsnd_send_completion(uint16 tick, uint8 packet_index)  void
76    rdpsnd_send_completion(uint16 tick, uint8 packet_index)
77  {  {
78          STREAM s;          STREAM s;
79    
80          s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);          s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);
81          out_uint16_le(s, tick+50);          out_uint16_le(s, tick);
82          out_uint8(s, packet_index);          out_uint8(s, packet_index);
83          out_uint8(s, 0);          out_uint8(s, 0);
84          s_mark_end(s);          s_mark_end(s);
85          rdpsnd_send(s);          rdpsnd_send(s);
86  }  }
87    
88  void  static void
89  rdpsnd_process_negotiate(STREAM in)  rdpsnd_process_negotiate(STREAM in)
90  {  {
91          unsigned int in_format_count, i;          unsigned int in_format_count, i;
92          WAVEFORMATEX *format;          WAVEFORMATEX *format;
93          STREAM out;          STREAM out;
94            BOOL device_available = False;
95            int readcnt;
96            int discardcnt;
97    
98          in_uint8s(in, 14);      /* flags, volume, pitch, UDP port */          in_uint8s(in, 14);      /* flags, volume, pitch, UDP port */
99          in_uint16_le(in, in_format_count);          in_uint16_le(in, in_format_count);
100          in_uint8s(in, 4);       /* pad, status, pad */          in_uint8s(in, 4);       /* pad, status, pad */
101    
102            if (current_driver->wave_out_open())
103            {
104                    current_driver->wave_out_close();
105                    device_available = True;
106            }
107    
108          format_count = 0;          format_count = 0;
109          if (s_check_rem(in, 18*in_format_count))          if (s_check_rem(in, 18 * in_format_count))
110          {          {
111                  for (i = 0; i < in_format_count; i++)                  for (i = 0; i < in_format_count; i++)
112                  {                  {
# Line 97  rdpsnd_process_negotiate(STREAM in) Line 119  rdpsnd_process_negotiate(STREAM in)
119                          in_uint16_le(in, format->wBitsPerSample);                          in_uint16_le(in, format->wBitsPerSample);
120                          in_uint16_le(in, format->cbSize);                          in_uint16_le(in, format->cbSize);
121    
122                          if (wave_out_format_supported(format))                          /* read in the buffer of unknown use */
123                            readcnt = format->cbSize;
124                            discardcnt = 0;
125                            if (format->cbSize > MAX_CBSIZE)
126                            {
127                                    fprintf(stderr, "cbSize too large for buffer: %d\n",
128                                            format->cbSize);
129                                    readcnt = MAX_CBSIZE;
130                                    discardcnt = format->cbSize - MAX_CBSIZE;
131                            }
132                            in_uint8a(in, format->cb, readcnt);
133                            in_uint8s(in, discardcnt);
134    
135                            if (device_available && current_driver->wave_out_format_supported(format))
136                          {                          {
137                                  format_count++;                                  format_count++;
138                                  if (format_count == MAX_FORMATS)                                  if (format_count == MAX_FORMATS)
# Line 106  rdpsnd_process_negotiate(STREAM in) Line 141  rdpsnd_process_negotiate(STREAM in)
141                  }                  }
142          }          }
143    
144          out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18*format_count);          out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18 * format_count);
145          out_uint32_le(out, 3);          /* flags */          out_uint32_le(out, 3);  /* flags */
146          out_uint32(out, 0xffffffff);    /* volume */          out_uint32(out, 0xffffffff);    /* volume */
147          out_uint32(out, 0);             /* pitch */          out_uint32(out, 0);     /* pitch */
148          out_uint16(out, 0);             /* UDP port */          out_uint16(out, 0);     /* UDP port */
149    
150          out_uint16_le(out, format_count);          out_uint16_le(out, format_count);
151          out_uint8(out, 0x95);           /* pad? */          out_uint8(out, 0x95);   /* pad? */
152          out_uint16_le(out, 2);          /* status */          out_uint16_le(out, 2);  /* status */
153          out_uint8(out, 0x77);           /* pad? */          out_uint8(out, 0x77);   /* pad? */
154    
155          for (i = 0; i < format_count; i++)          for (i = 0; i < format_count; i++)
156          {          {
# Line 126  rdpsnd_process_negotiate(STREAM in) Line 161  rdpsnd_process_negotiate(STREAM in)
161                  out_uint32_le(out, format->nAvgBytesPerSec);                  out_uint32_le(out, format->nAvgBytesPerSec);
162                  out_uint16_le(out, format->nBlockAlign);                  out_uint16_le(out, format->nBlockAlign);
163                  out_uint16_le(out, format->wBitsPerSample);                  out_uint16_le(out, format->wBitsPerSample);
164                  out_uint16(out, 0); /* cbSize */                  out_uint16(out, 0);     /* cbSize */
165          }          }
166    
167          s_mark_end(out);          s_mark_end(out);
168          rdpsnd_send(out);          rdpsnd_send(out);
169  }  }
170    
171  void  static void
172  rdpsnd_process_unknown6(STREAM in)  rdpsnd_process_servertick(STREAM in)
173  {  {
174          uint16 unknown1, unknown2;          uint16 tick1, tick2;
175          STREAM out;          STREAM out;
176    
177          /* in_uint8s(in, 4); unknown */          /* in_uint8s(in, 4); unknown */
178          in_uint16_le(in, unknown1);          in_uint16_le(in, tick1);
179          in_uint16_le(in, unknown2);          in_uint16_le(in, tick2);
180    
181          out = rdpsnd_init_packet(RDPSND_UNKNOWN6 | 0x2300, 4);          out = rdpsnd_init_packet(RDPSND_SERVERTICK | 0x2300, 4);
182          out_uint16_le(out, unknown1);          out_uint16_le(out, tick1);
183          out_uint16_le(out, unknown2);          out_uint16_le(out, tick2);
184          s_mark_end(out);          s_mark_end(out);
185          rdpsnd_send(out);          rdpsnd_send(out);
186  }  }
187    
188  void  static void
189  rdpsnd_process(STREAM s)  rdpsnd_process(STREAM s)
190  {  {
191          uint8 type;          uint8 type;
192          uint16 datalen;          uint16 datalen;
193            uint32 volume;
194          static uint16 tick, format;          static uint16 tick, format;
195          static uint8 packet_index;          static uint8 packet_index;
196          static BOOL awaiting_data_packet;          static BOOL awaiting_data_packet;
197    
198  #ifdef RDPSND_DEBUG  #ifdef RDPSND_DEBUG
199          printf("RDPSND recv:\n");          printf("RDPSND recv:\n");
200          hexdump(s->p, s->end-s->p);          hexdump(s->p, s->end - s->p);
201  #endif  #endif
202    
203          if (awaiting_data_packet)          if (awaiting_data_packet)
# Line 174  rdpsnd_process(STREAM s) Line 210  rdpsnd_process(STREAM s)
210    
211                  if (!device_open || (format != current_format))                  if (!device_open || (format != current_format))
212                  {                  {
213                          if (!device_open && !wave_out_open())                          if (!device_open && !current_driver->wave_out_open())
214                          {                          {
215                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
216                                  return;                                  return;
217                          }                          }
218                          if (!wave_out_set_format(&formats[format]))                          if (!current_driver->wave_out_set_format(&formats[format]))
219                          {                          {
220                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
221                                  wave_out_close();                                  current_driver->wave_out_close();
222                                  device_open = False;                                  device_open = False;
223                                  return;                                  return;
224                          }                          }
# Line 190  rdpsnd_process(STREAM s) Line 226  rdpsnd_process(STREAM s)
226                          current_format = format;                          current_format = format;
227                  }                  }
228    
229                  wave_out_write(s, tick, packet_index);                  current_driver->
230                            wave_out_write(rdpsnd_dsp_process
231                                           (s, current_driver, &formats[current_format]), tick,
232                                           packet_index);
233                  awaiting_data_packet = False;                  awaiting_data_packet = False;
234                  return;                  return;
235          }          }
236    
237          in_uint8(s, type);          in_uint8(s, type);
238          in_uint8s(s, 1); /* unknown? */          in_uint8s(s, 1);        /* unknown? */
239          in_uint16_le(s, datalen);          in_uint16_le(s, datalen);
240    
241          switch (type)          switch (type)
242          {          {
243          case RDPSND_WRITE:                  case RDPSND_WRITE:
244                  in_uint16_le(s, tick);                          in_uint16_le(s, tick);
245                  in_uint16_le(s, format);                          in_uint16_le(s, format);
246                  in_uint8(s, packet_index);                          in_uint8(s, packet_index);
247                  awaiting_data_packet = True;                          awaiting_data_packet = True;
248                  break;                          break;
249          case RDPSND_CLOSE:                  case RDPSND_CLOSE:
250                  wave_out_close();                          current_driver->wave_out_close();
251                  device_open = False;                          device_open = False;
252                  break;                          break;
253          case RDPSND_NEGOTIATE:                  case RDPSND_NEGOTIATE:
254                  rdpsnd_process_negotiate(s);                          rdpsnd_process_negotiate(s);
255                  break;                          break;
256          case RDPSND_UNKNOWN6:                  case RDPSND_SERVERTICK:
257                  rdpsnd_process_unknown6(s);                          rdpsnd_process_servertick(s);
258                  break;                          break;
259          case RDPSND_SET_VOLUME:                  case RDPSND_SET_VOLUME:
260                  /* uint32 volume */                          in_uint32(s, volume);
261                  break;                          if (device_open)
262          default:                          {
263                  unimpl("RDPSND packet type %d\n", type);                                  current_driver->wave_out_volume((volume & 0xffff),
264                  break;                                                                  (volume & 0xffff0000) >> 16);
265                            }
266                            break;
267                    default:
268                            unimpl("RDPSND packet type %d\n", type);
269                            break;
270          }          }
271  }  }
272    
# Line 232  rdpsnd_init(void) Line 276  rdpsnd_init(void)
276          rdpsnd_channel =          rdpsnd_channel =
277                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
278                                   rdpsnd_process);                                   rdpsnd_process);
279    
280          return (rdpsnd_channel != NULL);          return (rdpsnd_channel != NULL);
281  }  }
282    
283    BOOL
284    rdpsnd_auto_open(void)
285    {
286            current_driver = drivers;
287            while (current_driver != NULL)
288            {
289                    DEBUG(("trying %s...\n", current_driver->name));
290                    if (current_driver->wave_out_open())
291                    {
292                            DEBUG(("selected %s\n", current_driver->name));
293                            return True;
294                    }
295                    g_dsp_fd = 0;
296                    current_driver = current_driver->next;
297            }
298    
299            warning("no working audio-driver found\n");
300    
301            return False;
302    }
303    
304    void
305    rdpsnd_register_drivers(char *options)
306    {
307            struct audio_driver **reg;
308    
309            /* The order of registrations define the probe-order
310               when opening the device for the first time */
311            reg = &drivers;
312    #if defined(RDPSND_ALSA)
313            *reg = alsa_register(options);
314            reg = &((*reg)->next);
315    #endif
316    #if defined(RDPSND_SUN)
317            *reg = sun_register(options);
318            reg = &((*reg)->next);
319    #endif
320    #if defined(RDPSND_OSS)
321            *reg = oss_register(options);
322            reg = &((*reg)->next);
323    #endif
324    #if defined(RDPSND_SGI)
325            *reg = sgi_register(options);
326            reg = &((*reg)->next);
327    #endif
328    #if defined(RDPSND_LIBAO)
329            *reg = libao_register(options);
330            reg = &((*reg)->next);
331    #endif
332    }
333    
334    BOOL
335    rdpsnd_select_driver(char *driver, char *options)
336    {
337            static struct audio_driver auto_driver;
338            struct audio_driver *pos;
339    
340            drivers = NULL;
341            rdpsnd_register_drivers(options);
342    
343            if (!driver)
344            {
345                    auto_driver.wave_out_open = &rdpsnd_auto_open;
346                    current_driver = &auto_driver;
347                    return True;
348            }
349    
350            pos = drivers;
351            while (pos != NULL)
352            {
353                    if (!strcmp(pos->name, driver))
354                    {
355                            DEBUG(("selected %s\n", pos->name));
356                            current_driver = pos;
357                            return True;
358                    }
359                    pos = pos->next;
360            }
361            return False;
362    }
363    
364    void
365    rdpsnd_show_help(void)
366    {
367            struct audio_driver *pos;
368    
369            rdpsnd_register_drivers(NULL);
370    
371            pos = drivers;
372            while (pos != NULL)
373            {
374                    fprintf(stderr, "                     %s:\t%s\n", pos->name, pos->description);
375                    pos = pos->next;
376            }
377    }
378    
379    inline void
380    rdpsnd_play(void)
381    {
382            current_driver->wave_out_play();
383    }
384    
385    void
386    rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index)
387    {
388            struct audio_packet *packet = &packet_queue[queue_hi];
389            unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
390    
391            if (next_hi == queue_lo)
392            {
393                    error("No space to queue audio packet\n");
394                    return;
395            }
396    
397            queue_hi = next_hi;
398    
399            packet->s = *s;
400            packet->tick = tick;
401            packet->index = index;
402    
403            if (!g_dsp_busy)
404                    current_driver->wave_out_play();
405    }
406    
407    inline struct audio_packet *
408    rdpsnd_queue_current_packet(void)
409    {
410            return &packet_queue[queue_lo];
411    }
412    
413    inline BOOL
414    rdpsnd_queue_empty(void)
415    {
416            return (queue_lo == queue_hi);
417    }
418    
419    inline void
420    rdpsnd_queue_init(void)
421    {
422            queue_lo = queue_hi = 0;
423    }
424    
425    inline void
426    rdpsnd_queue_next(void)
427    {
428            xfree(packet_queue[queue_lo].s.data);
429            queue_lo = (queue_lo + 1) % MAX_QUEUE;
430    }
431    
432    inline int
433    rdpsnd_queue_next_tick(void)
434    {
435            if (((queue_lo + 1) % MAX_QUEUE) != queue_hi)
436            {
437                    return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick;
438            }
439            else
440            {
441                    return (packet_queue[queue_lo].tick + 65535) % 65536;
442            }
443    }

Legend:
Removed from v.474  
changed lines
  Added in v.1264

  ViewVC Help
Powered by ViewVC 1.1.26