/[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 1044 by stargo, Wed Feb 8 12:10:41 2006 UTC revision 1255 by stargo, Sun Sep 17 11:04:50 2006 UTC
# Line 20  Line 20 
20  */  */
21    
22  #include "rdesktop.h"  #include "rdesktop.h"
23    #include "rdpsnd.h"
24    
25  #define RDPSND_CLOSE            1  #define RDPSND_CLOSE            1
26  #define RDPSND_WRITE            2  #define RDPSND_WRITE            2
# Line 30  Line 31 
31  #define RDPSND_NEGOTIATE        7  #define RDPSND_NEGOTIATE        7
32    
33  #define MAX_FORMATS             10  #define MAX_FORMATS             10
34    #define MAX_QUEUE               10
35    
36    BOOL g_dsp_busy = False;
37    int g_dsp_fd;
38    
39  static VCHANNEL *rdpsnd_channel;  static VCHANNEL *rdpsnd_channel;
40    static struct audio_driver *drivers = NULL;
41    static struct audio_driver *current_driver = NULL;
42    
43  static BOOL device_open;  static BOOL device_open;
44  static WAVEFORMATEX formats[MAX_FORMATS];  static WAVEFORMATEX formats[MAX_FORMATS];
45  static unsigned int format_count;  static unsigned int format_count;
46  static unsigned int current_format;  static unsigned int current_format;
47    static unsigned int queue_hi, queue_lo;
48    static struct audio_packet packet_queue[MAX_QUEUE];
49    
50    void (*wave_out_play) (void);
51    
52  static STREAM  static STREAM
53  rdpsnd_init_packet(uint16 type, uint16 size)  rdpsnd_init_packet(uint16 type, uint16 size)
# Line 66  rdpsnd_send_completion(uint16 tick, uint Line 77  rdpsnd_send_completion(uint16 tick, uint
77          STREAM s;          STREAM s;
78    
79          s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);          s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);
80          out_uint16_le(s, tick + 50);          out_uint16_le(s, tick);
81          out_uint8(s, packet_index);          out_uint8(s, packet_index);
82          out_uint8(s, 0);          out_uint8(s, 0);
83          s_mark_end(s);          s_mark_end(s);
# Line 87  rdpsnd_process_negotiate(STREAM in) Line 98  rdpsnd_process_negotiate(STREAM in)
98          in_uint16_le(in, in_format_count);          in_uint16_le(in, in_format_count);
99          in_uint8s(in, 4);       /* pad, status, pad */          in_uint8s(in, 4);       /* pad, status, pad */
100    
101          if (wave_out_open())          if (current_driver->wave_out_open())
102          {          {
103                  wave_out_close();                  current_driver->wave_out_close();
104                  device_available = True;                  device_available = True;
105          }          }
106    
# Line 120  rdpsnd_process_negotiate(STREAM in) Line 131  rdpsnd_process_negotiate(STREAM in)
131                          in_uint8a(in, format->cb, readcnt);                          in_uint8a(in, format->cb, readcnt);
132                          in_uint8s(in, discardcnt);                          in_uint8s(in, discardcnt);
133    
134                          if (device_available && wave_out_format_supported(format))                          if (device_available && current_driver->wave_out_format_supported(format))
135                          {                          {
136                                  format_count++;                                  format_count++;
137                                  if (format_count == MAX_FORMATS)                                  if (format_count == MAX_FORMATS)
# Line 198  rdpsnd_process(STREAM s) Line 209  rdpsnd_process(STREAM s)
209    
210                  if (!device_open || (format != current_format))                  if (!device_open || (format != current_format))
211                  {                  {
212                          if (!device_open && !wave_out_open())                          if (!device_open && !current_driver->wave_out_open())
213                          {                          {
214                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
215                                  return;                                  return;
216                          }                          }
217                          if (!wave_out_set_format(&formats[format]))                          if (!current_driver->wave_out_set_format(&formats[format]))
218                          {                          {
219                                  rdpsnd_send_completion(tick, packet_index);                                  rdpsnd_send_completion(tick, packet_index);
220                                  wave_out_close();                                  current_driver->wave_out_close();
221                                  device_open = False;                                  device_open = False;
222                                  return;                                  return;
223                          }                          }
# Line 214  rdpsnd_process(STREAM s) Line 225  rdpsnd_process(STREAM s)
225                          current_format = format;                          current_format = format;
226                  }                  }
227    
228                  wave_out_write(s, tick, packet_index);                  current_driver->wave_out_write(s, tick, packet_index);
229                  awaiting_data_packet = False;                  awaiting_data_packet = False;
230                  return;                  return;
231          }          }
# Line 232  rdpsnd_process(STREAM s) Line 243  rdpsnd_process(STREAM s)
243                          awaiting_data_packet = True;                          awaiting_data_packet = True;
244                          break;                          break;
245                  case RDPSND_CLOSE:                  case RDPSND_CLOSE:
246                          wave_out_close();                          current_driver->wave_out_close();
247                          device_open = False;                          device_open = False;
248                          break;                          break;
249                  case RDPSND_NEGOTIATE:                  case RDPSND_NEGOTIATE:
# Line 245  rdpsnd_process(STREAM s) Line 256  rdpsnd_process(STREAM s)
256                          in_uint32(s, volume);                          in_uint32(s, volume);
257                          if (device_open)                          if (device_open)
258                          {                          {
259                                  wave_out_volume((volume & 0xffff), (volume & 0xffff0000) >> 16);                                  current_driver->wave_out_volume((volume & 0xffff),
260                                                                    (volume & 0xffff0000) >> 16);
261                          }                          }
262                          break;                          break;
263                  default:                  default:
# Line 260  rdpsnd_init(void) Line 272  rdpsnd_init(void)
272          rdpsnd_channel =          rdpsnd_channel =
273                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,                  channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
274                                   rdpsnd_process);                                   rdpsnd_process);
275    
276          return (rdpsnd_channel != NULL);          return (rdpsnd_channel != NULL);
277  }  }
278    
279    BOOL
280    rdpsnd_auto_open(void)
281    {
282            current_driver = drivers;
283            while (current_driver != NULL)
284            {
285                    DEBUG(("trying %s...\n", current_driver->name));
286                    if (current_driver->wave_out_open())
287                    {
288                            DEBUG(("selected %s\n", current_driver->name));
289                            return True;
290                    }
291                    g_dsp_fd = 0;
292                    current_driver = current_driver->next;
293            }
294    
295            warning("no working audio-driver found\n");
296    
297            return False;
298    }
299    
300    void
301    rdpsnd_register_drivers(char *options)
302    {
303            struct audio_driver **reg;
304    
305            /* The order of registrations define the probe-order
306               when opening the device for the first time */
307            reg = &drivers;
308    #if defined(RDPSND_ALSA)
309            *reg = alsa_register(options);
310            reg = &((*reg)->next);
311    #endif
312    #if defined(RDPSND_OSS)
313            *reg = oss_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_SGI)
321            *reg = sgi_register(options);
322            reg = &((*reg)->next);
323    #endif
324    #if defined(RDPSND_LIBAO)
325            *reg = libao_register(options);
326            reg = &((*reg)->next);
327    #endif
328    }
329    
330    BOOL
331    rdpsnd_select_driver(char *driver, char *options)
332    {
333            static struct audio_driver auto_driver;
334            struct audio_driver *pos;
335    
336            drivers = NULL;
337            rdpsnd_register_drivers(options);
338    
339            if (!driver)
340            {
341                    auto_driver.wave_out_open = &rdpsnd_auto_open;
342                    current_driver = &auto_driver;
343                    return True;
344            }
345    
346            pos = drivers;
347            while (pos != NULL)
348            {
349                    if (!strcmp(pos->name, driver))
350                    {
351                            DEBUG(("selected %s\n", pos->name));
352                            current_driver = pos;
353                            return True;
354                    }
355                    pos = pos->next;
356            }
357            return False;
358    }
359    
360    void
361    rdpsnd_show_help(void)
362    {
363            struct audio_driver *pos;
364    
365            rdpsnd_register_drivers(NULL);
366    
367            pos = drivers;
368            while (pos != NULL)
369            {
370                    fprintf(stderr, "                     %s:\t%s\n", pos->name, pos->description);
371                    pos = pos->next;
372            }
373    }
374    
375    inline void
376    rdpsnd_play(void)
377    {
378            current_driver->wave_out_play();
379    }
380    
381    void
382    rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index)
383    {
384            struct audio_packet *packet = &packet_queue[queue_hi];
385            unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
386    
387            if (next_hi == queue_lo)
388            {
389                    error("No space to queue audio packet\n");
390                    return;
391            }
392    
393            queue_hi = next_hi;
394    
395            packet->s = *s;
396            packet->tick = tick;
397            packet->index = index;
398            packet->s.p += 4;
399    
400            /* we steal the data buffer from s, give it a new one */
401            s->data = malloc(s->size);
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            free(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.1044  
changed lines
  Added in v.1255

  ViewVC Help
Powered by ViewVC 1.1.26