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

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

revision 833 by stargo, Tue Mar 8 03:33:36 2005 UTC revision 1289 by stargo, Sun Oct 1 22:27:11 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 - libao-driver     Sound Channel Process Functions - libao-driver
4     Copyright (C) Matthew Chapman 2003     Copyright (C) Matthew Chapman 2003
5     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6     Copyright (C) Michael Gernoth mike@zerfleddert.de 2005     Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006
7    
8     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 21  Line 21 
21  */  */
22    
23  #include "rdesktop.h"  #include "rdesktop.h"
24    #include "rdpsnd.h"
25    #include "rdpsnd_dsp.h"
26  #include <unistd.h>  #include <unistd.h>
27  #include <fcntl.h>  #include <fcntl.h>
28  #include <errno.h>  #include <errno.h>
29  #include <ao/ao.h>  #include <ao/ao.h>
30    #include <sys/time.h>
31    
32  #define MAX_QUEUE       10  #define WAVEOUTBUF      512
33    
34  int g_dsp_fd;  static ao_device *o_device = NULL;
35  ao_device *o_device = NULL;  static int default_driver;
36  int default_driver;  static BOOL reopened;
37  BOOL g_dsp_busy = False;  static char *libao_device = NULL;
 static short g_samplewidth;  
   
 static struct audio_packet  
 {  
         struct stream s;  
         uint16 tick;  
         uint8 index;  
 } packet_queue[MAX_QUEUE];  
 static unsigned int queue_hi, queue_lo;  
38    
39  BOOL  BOOL
40  wave_out_open(void)  libao_open(void)
41  {  {
42          ao_sample_format format;          ao_sample_format format;
43    
44          ao_initialize();          ao_initialize();
45          default_driver = ao_default_driver_id();  
46            if (libao_device)
47            {
48                    default_driver = ao_driver_id(libao_device);
49            }
50            else
51            {
52                    default_driver = ao_default_driver_id();
53            }
54    
55          format.bits = 16;          format.bits = 16;
56          format.channels = 2;          format.channels = 2;
57          format.rate = 44100;          format.rate = 44100;
58          format.byte_format = AO_FMT_LITTLE;          format.byte_format = AO_FMT_NATIVE;
59    
60    
61          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
62          if (o_device == NULL)          if (o_device == NULL)
# Line 62  wave_out_open(void) Line 65  wave_out_open(void)
65          }          }
66    
67          g_dsp_fd = 0;          g_dsp_fd = 0;
68          queue_lo = queue_hi = 0;          rdpsnd_queue_init();
69    
70            reopened = True;
71    
72          return True;          return True;
73  }  }
74    
75  void  void
76  wave_out_close(void)  libao_close(void)
77  {  {
78          /* Ack all remaining packets */          /* Ack all remaining packets */
79          while (queue_lo != queue_hi)          while (!rdpsnd_queue_empty())
80          {          {
81                  rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index);                  rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,
82                  free(packet_queue[queue_lo].s.data);                                         rdpsnd_queue_current_packet()->index);
83                  queue_lo = (queue_lo + 1) % MAX_QUEUE;                  rdpsnd_queue_next();
84          }          }
85    
86          if (o_device != NULL)          if (o_device != NULL)
87                  ao_close(o_device);                  ao_close(o_device);
         ao_shutdown();  
 }  
88    
89  BOOL          ao_shutdown();
 wave_out_format_supported(WAVEFORMATEX * pwfx)  
 {  
         if (pwfx->wFormatTag != WAVE_FORMAT_PCM)  
                 return False;  
         if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))  
                 return False;  
         if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))  
                 return False;  
         /* The only common denominator between libao output drivers is a sample-rate of  
            44100, windows gives a max of 22050. we need to upsample that...  
            TODO: support 11025, too */  
         if (pwfx->nSamplesPerSec != 22050)  
                 return False;  
   
         return True;  
90  }  }
91    
92  BOOL  BOOL
93  wave_out_set_format(WAVEFORMATEX * pwfx)  libao_set_format(WAVEFORMATEX * pwfx)
94  {  {
95          ao_sample_format format;          ao_sample_format format;
96    
         printf("%d\n",pwfx->wBitsPerSample);  
97          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
98          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
99          format.rate = 44100;          format.rate = 44100;
100          format.byte_format = AO_FMT_LITTLE;          format.byte_format = AO_FMT_NATIVE;
   
         g_samplewidth = pwfx->wBitsPerSample / 8;  
101    
102          if(o_device != NULL)          if (o_device != NULL)
103                  ao_close(o_device);                  ao_close(o_device);
104    
105          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
# Line 123  wave_out_set_format(WAVEFORMATEX * pwfx) Line 108  wave_out_set_format(WAVEFORMATEX * pwfx)
108                  return False;                  return False;
109          }          }
110    
111            if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
112            {
113                    return False;
114            }
115    
116            reopened = True;
117    
118          return True;          return True;
119  }  }
120    
121  void  void
122  wave_out_volume(uint16 left, uint16 right)  libao_play(void)
123  {  {
124  }          struct audio_packet *packet;
125            STREAM out;
126            int len;
127            static long prev_s, prev_us;
128            unsigned int duration;
129            struct timeval tv;
130            int next_tick;
131    
132  void          if (reopened)
133  wave_out_write(STREAM s, uint16 tick, uint8 index)          {
134  {                  reopened = False;
135          struct audio_packet *packet = &packet_queue[queue_hi];                  gettimeofday(&tv, NULL);
136          unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;                  prev_s = tv.tv_sec;
137                    prev_us = tv.tv_usec;
138            }
139    
140          if (next_hi == queue_lo)          if (rdpsnd_queue_empty())
141          {          {
142                  error("No space to queue audio packet\n");                  g_dsp_busy = 0;
143                  return;                  return;
144          }          }
145    
146          queue_hi = next_hi;          packet = rdpsnd_queue_current_packet();
147            out = &packet->s;
148    
149          packet->s = *s;          next_tick = rdpsnd_queue_next_tick();
         packet->tick = tick;  
         packet->index = index;  
         packet->s.p += 4;  
150    
151          /* we steal the data buffer from s, give it a new one */          len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF;
152          s->data = malloc(s->size);          ao_play(o_device, (char *) out->p, len);
153            out->p += len;
154    
155          if (!g_dsp_busy)          gettimeofday(&tv, NULL);
                 wave_out_play();  
 }  
156    
157  void          duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
 wave_out_play(void)  
 {  
         struct audio_packet *packet;  
         STREAM out;  
         unsigned char expanded[8];  
158    
159          while (1)          if (packet->tick > next_tick)
160                    next_tick += 65536;
161    
162            if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
163          {          {
164                  if (queue_lo == queue_hi)                  prev_s = tv.tv_sec;
165                    prev_us = tv.tv_usec;
166    
167                    if (abs((next_tick - packet->tick) - duration) > 20)
168                  {                  {
169                          g_dsp_busy = 0;                          DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
170                          return;                          DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
171                                   (packet->tick + duration) % 65536, next_tick % 65536));
172                  }                  }
173    
174                  packet = &packet_queue[queue_lo];                  rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);
175                  out = &packet->s;                  rdpsnd_queue_next();
176            }
177    
178            g_dsp_busy = 1;
179            return;
180    }
181    
182    struct audio_driver *
183    libao_register(char *options)
184    {
185            static struct audio_driver libao_driver;
186            struct ao_info *libao_info;
187            static char description[101];
188    
189            libao_driver.wave_out_write = rdpsnd_queue_write;
190            libao_driver.wave_out_open = libao_open;
191            libao_driver.wave_out_close = libao_close;
192            libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
193            libao_driver.wave_out_set_format = libao_set_format;
194            libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
195            libao_driver.wave_out_play = libao_play;
196            libao_driver.name = xstrdup("libao");
197            libao_driver.description = description;
198            libao_driver.need_byteswap_on_be = 1;
199            libao_driver.need_resampling = 1;
200            libao_driver.next = NULL;
201    
202                  /* Resample 22050 -> 44100 */          ao_initialize();
                 /* TODO: Do this for 11025, too... */  
                 memcpy(&expanded[0],out->p,g_samplewidth);  
                 memcpy(&expanded[2*g_samplewidth],out->p,g_samplewidth);  
                 out->p += 2;  
                 memcpy(&expanded[1*g_samplewidth],out->p,g_samplewidth);  
                 memcpy(&expanded[3*g_samplewidth],out->p,g_samplewidth);  
                 out->p += 2;  
203    
204                  ao_play(o_device, expanded, g_samplewidth*4);          libao_info = ao_driver_info(ao_default_driver_id());
205    
206                  if (out->p == out->end)          if (libao_info)
207                  {          {
208                          rdpsnd_send_completion(packet->tick, packet->index);                  snprintf(description, 100, "libao output driver, default device: %s",
209                          free(out->data);                           libao_info->short_name);
210                          queue_lo = (queue_lo + 1) % MAX_QUEUE;          }
211                  } else {          else
212                          g_dsp_busy = 1;          {
213                          return;                  snprintf(description, 100, "libao output driver, default device: none");
                 }  
214          }          }
215    
216            ao_shutdown();
217    
218            if (options)
219            {
220                    libao_device = xstrdup(options);
221            }
222    
223            return &libao_driver;
224  }  }

Legend:
Removed from v.833  
changed lines
  Added in v.1289

  ViewVC Help
Powered by ViewVC 1.1.26