/[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 1373 by stargo, Mon Jan 8 12:38:34 2007 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- 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-2007
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-2006     Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2007
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 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;
36  static int samplerate;  static RD_BOOL reopened;
 static int audiochannels;  
 static BOOL reopened;  
 static short samplewidth;  
37  static char *libao_device = NULL;  static char *libao_device = NULL;
38    
39  BOOL  void libao_play(void);
40    
41    void
42    libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
43    {
44            /* We need to be called rather often... */
45            FD_SET(0, wfds);
46    }
47    
48    void
49    libao_check_fds(fd_set * rfds, fd_set * wfds)
50    {
51            if (o_device == NULL)
52                    return;
53    
54            if (!rdpsnd_queue_empty())
55                    libao_play();
56    }
57    
58    RD_BOOL
59  libao_open(void)  libao_open(void)
60  {  {
61          ao_sample_format format;          ao_sample_format format;
# Line 57  libao_open(void) Line 73  libao_open(void)
73    
74          format.bits = 16;          format.bits = 16;
75          format.channels = 2;          format.channels = 2;
         audiochannels = 2;  
76          format.rate = 44100;          format.rate = 44100;
77          samplerate = 44100;          format.byte_format = AO_FMT_NATIVE;
78          format.byte_format = AO_FMT_LITTLE;  
79    
80          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
81          if (o_device == NULL)          if (o_device == NULL)
# Line 68  libao_open(void) Line 83  libao_open(void)
83                  return False;                  return False;
84          }          }
85    
         g_dsp_fd = 0;  
         rdpsnd_queue_init();  
   
86          reopened = True;          reopened = True;
87    
88          return True;          return True;
# Line 82  libao_close(void) Line 94  libao_close(void)
94          /* Ack all remaining packets */          /* Ack all remaining packets */
95          while (!rdpsnd_queue_empty())          while (!rdpsnd_queue_empty())
96          {          {
97                  rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,                  rdpsnd_queue_next(0);
                                        rdpsnd_queue_current_packet()->index);  
                 rdpsnd_queue_next();  
98          }          }
99    
100          if (o_device != NULL)          if (o_device != NULL)
101                  ao_close(o_device);                  ao_close(o_device);
102    
103          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;  
104    
105          return True;          ao_shutdown();
106  }  }
107    
108  BOOL  RD_BOOL
109  libao_set_format(WAVEFORMATEX * pwfx)  libao_set_format(RD_WAVEFORMATEX * pwfx)
110  {  {
111          ao_sample_format format;          ao_sample_format format;
112    
113          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
114          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
         audiochannels = pwfx->nChannels;  
115          format.rate = 44100;          format.rate = 44100;
116          samplerate = pwfx->nSamplesPerSec;          format.byte_format = AO_FMT_NATIVE;
         format.byte_format = AO_FMT_LITTLE;  
   
         samplewidth = pwfx->wBitsPerSample / 8;  
117    
118          if (o_device != NULL)          if (o_device != NULL)
119                  ao_close(o_device);                  ao_close(o_device);
# Line 133  libao_set_format(WAVEFORMATEX * pwfx) Line 124  libao_set_format(WAVEFORMATEX * pwfx)
124                  return False;                  return False;
125          }          }
126    
127            if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
128            {
129                    return False;
130            }
131    
132          reopened = True;          reopened = True;
133    
134          return True;          return True;
# Line 143  libao_play(void) Line 139  libao_play(void)
139  {  {
140          struct audio_packet *packet;          struct audio_packet *packet;
141          STREAM out;          STREAM out;
142          char outbuf[WAVEOUTBUF];          int len;
         int offset, len, i;  
143          static long prev_s, prev_us;          static long prev_s, prev_us;
144          unsigned int duration;          unsigned int duration;
145          struct timeval tv;          struct timeval tv;
# Line 158  libao_play(void) Line 153  libao_play(void)
153                  prev_us = tv.tv_usec;                  prev_us = tv.tv_usec;
154          }          }
155    
156            /* We shouldn't be called if the queue is empty, but still */
157          if (rdpsnd_queue_empty())          if (rdpsnd_queue_empty())
         {  
                 g_dsp_busy = 0;  
158                  return;                  return;
         }  
159    
160          packet = rdpsnd_queue_current_packet();          packet = rdpsnd_queue_current_packet();
161          out = &packet->s;          out = &packet->s;
162    
163          next_tick = rdpsnd_queue_next_tick();          next_tick = rdpsnd_queue_next_tick();
164    
165          len = 0;          len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
166            ao_play(o_device, (char *) out->p, len);
167          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);  
168    
169          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
170    
# Line 212  libao_play(void) Line 175  libao_play(void)
175    
176          if ((out->p == out->end) || duration > next_tick - packet->tick + 500)          if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
177          {          {
178                    unsigned int delay_us;
179    
180                  prev_s = tv.tv_sec;                  prev_s = tv.tv_sec;
181                  prev_us = tv.tv_usec;                  prev_us = tv.tv_usec;
182    
# Line 222  libao_play(void) Line 187  libao_play(void)
187                                 (packet->tick + duration) % 65536, next_tick % 65536));                                 (packet->tick + duration) % 65536, next_tick % 65536));
188                  }                  }
189    
190                  rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);                  delay_us = ((out->size / 4) * (1000000 / 44100));
                 rdpsnd_queue_next();  
         }  
191    
192          g_dsp_busy = 1;                  rdpsnd_queue_next(delay_us);
193          return;          }
194  }  }
195    
196  struct audio_driver *  struct audio_driver *
197  libao_register(char *options)  libao_register(char *options)
198  {  {
199          static struct audio_driver libao_driver;          static struct audio_driver libao_driver;
         struct ao_info *libao_info;  
         static char description[101];  
200    
201          libao_driver.wave_out_write = rdpsnd_queue_write;          memset(&libao_driver, 0, sizeof(libao_driver));
202    
203            libao_driver.name = "libao";
204            libao_driver.description = "libao output driver, default device: system dependent";
205    
206            libao_driver.add_fds = libao_add_fds;
207            libao_driver.check_fds = libao_check_fds;
208    
209          libao_driver.wave_out_open = libao_open;          libao_driver.wave_out_open = libao_open;
210          libao_driver.wave_out_close = libao_close;          libao_driver.wave_out_close = libao_close;
211          libao_driver.wave_out_format_supported = libao_format_supported;          libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
212          libao_driver.wave_out_set_format = libao_set_format;          libao_driver.wave_out_set_format = libao_set_format;
213          libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;          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());  
214    
215          if (libao_info)          libao_driver.need_byteswap_on_be = 1;
216          {          libao_driver.need_resampling = 1;
                 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();  
217    
218          if (options)          if (options)
219          {          {

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

  ViewVC Help
Powered by ViewVC 1.1.26