/[rdesktop]/jpeg/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 /jpeg/rdesktop/trunk/rdpsnd.c

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

revision 963 by astrand, Wed Aug 3 10:56:16 2005 UTC revision 1271 by stargo, Mon Sep 18 21:42:50 2006 UTC
# 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  static STREAM  static STREAM
54  rdpsnd_init_packet(uint16 type, uint16 size)  rdpsnd_init_packet(uint16 type, uint16 size)
# Line 66  rdpsnd_send_completion(uint16 tick, uint Line 78  rdpsnd_send_completion(uint16 tick, uint
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);
# Line 87  rdpsnd_process_negotiate(STREAM in) Line 99  rdpsnd_process_negotiate(STREAM in)
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 (wave_out_open())          if (current_driver->wave_out_open())
103          {          {
104                  wave_out_close();                  current_driver->wave_out_close();
105                  device_available = True;                  device_available = True;
106          }          }
107    
# Line 120  rdpsnd_process_negotiate(STREAM in) Line 132  rdpsnd_process_negotiate(STREAM in)
132                          in_uint8a(in, format->cb, readcnt);                          in_uint8a(in, format->cb, readcnt);
133                          in_uint8s(in, discardcnt);                          in_uint8s(in, discardcnt);
134    
135                          if (device_available && wave_out_format_supported(format))                          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 157  rdpsnd_process_negotiate(STREAM in) Line 169  rdpsnd_process_negotiate(STREAM in)
169  }  }
170    
171  static 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  }  }
# Line 182  rdpsnd_process(STREAM s) Line 194  rdpsnd_process(STREAM s)
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            static unsigned char missing_bytes[4] = { 0, 0, 0, 0 };
198    
199  #ifdef RDPSND_DEBUG  #ifdef RDPSND_DEBUG
200          printf("RDPSND recv:\n");          printf("RDPSND recv:\n");
# Line 198  rdpsnd_process(STREAM s) Line 211  rdpsnd_process(STREAM s)
211    
212                  if (!device_open || (format != current_format))                  if (!device_open || (format != current_format))
213                  {                  {
214                          if (!device_open && !wave_out_open())                          if (!device_open && !current_driver->wave_out_open())
215                          {                          {
216                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
217                                  return;                                  return;
218                          }                          }
219                          if (!wave_out_set_format(&formats[format]))                          if (!current_driver->wave_out_set_format(&formats[format]))
220                          {                          {
221                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
222                                  wave_out_close();                                  current_driver->wave_out_close();
223                                  device_open = False;                                  device_open = False;
224                                  return;                                  return;
225                          }                          }
# Line 214  rdpsnd_process(STREAM s) Line 227  rdpsnd_process(STREAM s)
227                          current_format = format;                          current_format = format;
228                  }                  }
229    
230                  wave_out_write(s, tick, packet_index);                  /* Insert the 4 missing bytes retrieved from last RDPSND_WRITE */
231                    memcpy(s->data, missing_bytes, 4);
232    
233                    current_driver->
234                            wave_out_write(rdpsnd_dsp_process
235                                           (s, current_driver, &formats[current_format]), tick,
236                                           packet_index);
237                  awaiting_data_packet = False;                  awaiting_data_packet = False;
238                  return;                  return;
239          }          }
# Line 229  rdpsnd_process(STREAM s) Line 248  rdpsnd_process(STREAM s)
248                          in_uint16_le(s, tick);                          in_uint16_le(s, tick);
249                          in_uint16_le(s, format);                          in_uint16_le(s, format);
250                          in_uint8(s, packet_index);                          in_uint8(s, packet_index);
251                            /* Here are our lost bytes, but why? */
252                            memcpy(missing_bytes, s->end - 4, 4);
253                          awaiting_data_packet = True;                          awaiting_data_packet = True;
254                          break;                          break;
255                  case RDPSND_CLOSE:                  case RDPSND_CLOSE:
256                          wave_out_close();                          current_driver->wave_out_close();
257                          device_open = False;                          device_open = False;
258                          break;                          break;
259                  case RDPSND_NEGOTIATE:                  case RDPSND_NEGOTIATE:
260                          rdpsnd_process_negotiate(s);                          rdpsnd_process_negotiate(s);
261                          break;                          break;
262                  case RDPSND_UNKNOWN6:                  case RDPSND_SERVERTICK:
263                          rdpsnd_process_unknown6(s);                          rdpsnd_process_servertick(s);
264                          break;                          break;
265                  case RDPSND_SET_VOLUME:                  case RDPSND_SET_VOLUME:
266                          in_uint32(s, volume);                          in_uint32(s, volume);
267                          if (device_open)                          if (device_open)
268                          {                          {
269                                  wave_out_volume((volume & 0xffff), (volume & 0xffff0000) >> 16);                                  current_driver->wave_out_volume((volume & 0xffff),
270                                                                    (volume & 0xffff0000) >> 16);
271                          }                          }
272                          break;                          break;
273                  default:                  default:
# Line 260  rdpsnd_init(void) Line 282  rdpsnd_init(void)
282          rdpsnd_channel =          rdpsnd_channel =
283                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
284                                   rdpsnd_process);                                   rdpsnd_process);
285    
286          return (rdpsnd_channel != NULL);          return (rdpsnd_channel != NULL);
287  }  }
288    
289    BOOL
290    rdpsnd_auto_open(void)
291    {
292            static BOOL failed = False;
293    
294            if (!failed)
295            {
296                    struct audio_driver *auto_driver = current_driver;
297    
298                    current_driver = drivers;
299                    while (current_driver != NULL)
300                    {
301                            DEBUG(("trying %s...\n", current_driver->name));
302                            if (current_driver->wave_out_open())
303                            {
304                                    DEBUG(("selected %s\n", current_driver->name));
305                                    return True;
306                            }
307                            g_dsp_fd = 0;
308                            current_driver = current_driver->next;
309                    }
310    
311                    warning("no working audio-driver found\n");
312                    failed = True;
313                    current_driver = auto_driver;
314            }
315    
316            return False;
317    }
318    
319    void
320    rdpsnd_register_drivers(char *options)
321    {
322            struct audio_driver **reg;
323    
324            /* The order of registrations define the probe-order
325               when opening the device for the first time */
326            reg = &drivers;
327    #if defined(RDPSND_ALSA)
328            *reg = alsa_register(options);
329            reg = &((*reg)->next);
330    #endif
331    #if defined(RDPSND_SUN)
332            *reg = sun_register(options);
333            reg = &((*reg)->next);
334    #endif
335    #if defined(RDPSND_OSS)
336            *reg = oss_register(options);
337            reg = &((*reg)->next);
338    #endif
339    #if defined(RDPSND_SGI)
340            *reg = sgi_register(options);
341            reg = &((*reg)->next);
342    #endif
343    #if defined(RDPSND_LIBAO)
344            *reg = libao_register(options);
345            reg = &((*reg)->next);
346    #endif
347    }
348    
349    BOOL
350    rdpsnd_select_driver(char *driver, char *options)
351    {
352            static struct audio_driver auto_driver;
353            struct audio_driver *pos;
354    
355            drivers = NULL;
356            rdpsnd_register_drivers(options);
357    
358            if (!driver)
359            {
360                    auto_driver.wave_out_open = &rdpsnd_auto_open;
361                    current_driver = &auto_driver;
362                    return True;
363            }
364    
365            pos = drivers;
366            while (pos != NULL)
367            {
368                    if (!strcmp(pos->name, driver))
369                    {
370                            DEBUG(("selected %s\n", pos->name));
371                            current_driver = pos;
372                            return True;
373                    }
374                    pos = pos->next;
375            }
376            return False;
377    }
378    
379    void
380    rdpsnd_show_help(void)
381    {
382            struct audio_driver *pos;
383    
384            rdpsnd_register_drivers(NULL);
385    
386            pos = drivers;
387            while (pos != NULL)
388            {
389                    fprintf(stderr, "                     %s:\t%s\n", pos->name, pos->description);
390                    pos = pos->next;
391            }
392    }
393    
394    inline void
395    rdpsnd_play(void)
396    {
397            current_driver->wave_out_play();
398    }
399    
400    void
401    rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index)
402    {
403            struct audio_packet *packet = &packet_queue[queue_hi];
404            unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
405    
406            if (next_hi == queue_lo)
407            {
408                    error("No space to queue audio packet\n");
409                    return;
410            }
411    
412            queue_hi = next_hi;
413    
414            packet->s = *s;
415            packet->tick = tick;
416            packet->index = index;
417    
418            if (!g_dsp_busy)
419                    current_driver->wave_out_play();
420    }
421    
422    inline struct audio_packet *
423    rdpsnd_queue_current_packet(void)
424    {
425            return &packet_queue[queue_lo];
426    }
427    
428    inline BOOL
429    rdpsnd_queue_empty(void)
430    {
431            return (queue_lo == queue_hi);
432    }
433    
434    inline void
435    rdpsnd_queue_init(void)
436    {
437            queue_lo = queue_hi = 0;
438    }
439    
440    inline void
441    rdpsnd_queue_next(void)
442    {
443            xfree(packet_queue[queue_lo].s.data);
444            queue_lo = (queue_lo + 1) % MAX_QUEUE;
445    }
446    
447    inline int
448    rdpsnd_queue_next_tick(void)
449    {
450            if (((queue_lo + 1) % MAX_QUEUE) != queue_hi)
451            {
452                    return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick;
453            }
454            else
455            {
456                    return (packet_queue[queue_lo].tick + 65535) % 65536;
457            }
458    }

Legend:
Removed from v.963  
changed lines
  Added in v.1271

  ViewVC Help
Powered by ViewVC 1.1.26