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

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

revision 1268 by stargo, Mon Sep 18 09:34:34 2006 UTC revision 1346 by ossman_, Thu Dec 7 15:23:45 2006 UTC
# Line 29  Line 29 
29  #include <ao/ao.h>  #include <ao/ao.h>
30  #include <sys/time.h>  #include <sys/time.h>
31    
32  #define WAVEOUTBUF      16  #define WAVEOUTLEN      16
33    
34  static ao_device *o_device = NULL;  static ao_device *o_device = NULL;
35  static int default_driver;  static int default_driver;
 static int samplerate;  
 static int audiochannels;  
36  static BOOL reopened;  static BOOL reopened;
 static short samplewidth;  
37  static char *libao_device = NULL;  static char *libao_device = NULL;
38    
39    void libao_play(void);
40    
41    void
42    libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
43    {
44    }
45    
46    void
47    libao_check_fds(fd_set * rfds, fd_set * wfds)
48    {
49            if (o_device == NULL)
50                    return;
51    
52            if (!rdpsnd_queue_empty())
53                    libao_play();
54    }
55    
56  BOOL  BOOL
57  libao_open(void)  libao_open(void)
58  {  {
# Line 57  libao_open(void) Line 71  libao_open(void)
71    
72          format.bits = 16;          format.bits = 16;
73          format.channels = 2;          format.channels = 2;
         audiochannels = 2;  
74          format.rate = 44100;          format.rate = 44100;
75          samplerate = 44100;          format.byte_format = AO_FMT_NATIVE;
76          format.byte_format = AO_FMT_LITTLE;  
77    
78          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
79          if (o_device == NULL)          if (o_device == NULL)
# Line 68  libao_open(void) Line 81  libao_open(void)
81                  return False;                  return False;
82          }          }
83    
         g_dsp_fd = 0;  
         rdpsnd_queue_init();  
   
84          reopened = True;          reopened = True;
85    
86          return True;          return True;
# Line 82  libao_close(void) Line 92  libao_close(void)
92          /* Ack all remaining packets */          /* Ack all remaining packets */
93          while (!rdpsnd_queue_empty())          while (!rdpsnd_queue_empty())
94          {          {
95                  rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,                  rdpsnd_queue_next(0);
                                        rdpsnd_queue_current_packet()->index);  
                 rdpsnd_queue_next();  
96          }          }
97    
98          if (o_device != NULL)          if (o_device != NULL)
99                  ao_close(o_device);                  ao_close(o_device);
100    
101          ao_shutdown();          o_device = NULL;
 }  
   
 BOOL  
 libao_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, we need to upsample 22050 to it */  
         if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))  
                 return False;  
102    
103          return True;          ao_shutdown();
104  }  }
105    
106  BOOL  BOOL
# Line 117  libao_set_format(WAVEFORMATEX * pwfx) Line 110  libao_set_format(WAVEFORMATEX * pwfx)
110    
111          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
112          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
         audiochannels = pwfx->nChannels;  
113          format.rate = 44100;          format.rate = 44100;
114          samplerate = pwfx->nSamplesPerSec;          format.byte_format = AO_FMT_NATIVE;
         format.byte_format = AO_FMT_LITTLE;  
   
         samplewidth = pwfx->wBitsPerSample / 8;  
115    
116          if (o_device != NULL)          if (o_device != NULL)
117                  ao_close(o_device);                  ao_close(o_device);
# Line 133  libao_set_format(WAVEFORMATEX * pwfx) Line 122  libao_set_format(WAVEFORMATEX * pwfx)
122                  return False;                  return False;
123          }          }
124    
125            if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
126            {
127                    return False;
128            }
129    
130          reopened = True;          reopened = True;
131    
132          return True;          return True;
# Line 143  libao_play(void) Line 137  libao_play(void)
137  {  {
138          struct audio_packet *packet;          struct audio_packet *packet;
139          STREAM out;          STREAM out;
140          char outbuf[WAVEOUTBUF];          int len;
         int offset, len, i;  
141          static long prev_s, prev_us;          static long prev_s, prev_us;
142          unsigned int duration;          unsigned int duration;
143          struct timeval tv;          struct timeval tv;
# Line 158  libao_play(void) Line 151  libao_play(void)
151                  prev_us = tv.tv_usec;                  prev_us = tv.tv_usec;
152          }          }
153    
154            /* We shouldn't be called if the queue is empty, but still */
155          if (rdpsnd_queue_empty())          if (rdpsnd_queue_empty())
         {  
                 g_dsp_busy = 0;  
156                  return;                  return;
         }  
157    
158          packet = rdpsnd_queue_current_packet();          packet = rdpsnd_queue_current_packet();
159          out = &packet->s;          out = &packet->s;
160    
161          next_tick = rdpsnd_queue_next_tick();          next_tick = rdpsnd_queue_next_tick();
162    
163          len = 0;          len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
164            ao_play(o_device, (char *) out->p, len);
165          if (samplerate == 22050)          out->p += len;
         {  
                 /* Resample to 44100 */  
                 for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - samplewidth))) && (out->p < out->end);  
                      i++)  
                 {  
                         /* On a stereo-channel we must make sure that left and right  
                            does not get mixed up, so we need to expand the sample-  
                            data with channels in mind: 1234 -> 12123434  
                            If we have a mono-channel, we can expand the data by simply  
                            doubling the sample-data: 1234 -> 11223344 */  
                         if (audiochannels == 2)  
                                 offset = ((i * 2) - (i & 1)) * samplewidth;  
                         else  
                                 offset = (i * 2) * samplewidth;  
   
                         memcpy(&outbuf[offset], out->p, samplewidth);  
                         memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth);  
   
                         out->p += samplewidth;  
                         len += 2 * samplewidth;  
                 }  
         }  
         else  
         {  
                 len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF;  
                 memcpy(outbuf, out->p, len);  
                 out->p += len;  
         }  
   
         ao_play(o_device, outbuf, len);  
166    
167          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
168    
# Line 222  libao_play(void) Line 183  libao_play(void)
183                                 (packet->tick + duration) % 65536, next_tick % 65536));                                 (packet->tick + duration) % 65536, next_tick % 65536));
184                  }                  }
185    
186                  rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);                  rdpsnd_queue_next(duration);
                 rdpsnd_queue_next();  
187          }          }
   
         g_dsp_busy = 1;  
         return;  
188  }  }
189    
190    static struct audio_driver libao_driver = {
191            .name = "libao",
192            .description = "libao output driver, default device: system dependent",
193    
194            .add_fds = libao_add_fds,
195            .check_fds = libao_check_fds,
196    
197            .wave_out_open = libao_open,
198            .wave_out_close = libao_close,
199            .wave_out_format_supported = rdpsnd_dsp_resample_supported,
200            .wave_out_set_format = libao_set_format,
201            .wave_out_volume = rdpsnd_dsp_softvol_set,
202    
203            .need_byteswap_on_be = 1,
204            .need_resampling = 1,
205    };
206    
207  struct audio_driver *  struct audio_driver *
208  libao_register(char *options)  libao_register(char *options)
209  {  {
         static struct audio_driver libao_driver;  
         struct ao_info *libao_info;  
         static char description[101];  
   
         libao_driver.wave_out_write = rdpsnd_queue_write;  
         libao_driver.wave_out_open = libao_open;  
         libao_driver.wave_out_close = libao_close;  
         libao_driver.wave_out_format_supported = libao_format_supported;  
         libao_driver.wave_out_set_format = libao_set_format;  
         libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;  
         libao_driver.wave_out_play = libao_play;  
         libao_driver.name = xstrdup("libao");  
         libao_driver.description = description;  
         libao_driver.need_byteswap_on_be = 0;  
         libao_driver.next = NULL;  
   
         ao_initialize();  
   
         libao_info = ao_driver_info(ao_default_driver_id());  
   
         if (libao_info)  
         {  
                 snprintf(description, 100, "libao output driver, default device: %s",  
                                 libao_info->short_name);  
         }  
         else  
         {  
                 snprintf(description, 100, "libao output driver, default device: none");  
         }  
   
         ao_shutdown();  
   
210          if (options)          if (options)
211          {          {
212                  libao_device = xstrdup(options);                  libao_device = xstrdup(options);

Legend:
Removed from v.1268  
changed lines
  Added in v.1346

  ViewVC Help
Powered by ViewVC 1.1.26