/[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 836 by stargo, Tue Mar 8 12:09:20 2005 UTC revision 1475 by jsorg71, Fri Jul 11 03:51:23 2008 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-2008
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-2008
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 WAVEOUTLEN      16
 #define WAVEOUTBUF      32  
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 RD_BOOL reopened;
37  int g_samplerate;  static char *libao_device = NULL;
 BOOL g_dsp_busy = False;  
 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  void libao_play(void);
40  wave_out_open(void)  
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            if (o_device != NULL && !rdpsnd_queue_empty())
46                    FD_SET(0, wfds);
47    }
48    
49    void
50    libao_check_fds(fd_set * rfds, fd_set * wfds)
51    {
52            if (o_device == NULL)
53                    return;
54    
55            if (!rdpsnd_queue_empty())
56                    libao_play();
57    }
58    
59    RD_BOOL
60    libao_open(void)
61  {  {
62          ao_sample_format format;          ao_sample_format format;
63    
64          ao_initialize();          ao_initialize();
65          default_driver = ao_default_driver_id();  
66            if (libao_device)
67            {
68                    default_driver = ao_driver_id(libao_device);
69            }
70            else
71            {
72                    default_driver = ao_default_driver_id();
73            }
74    
75          format.bits = 16;          format.bits = 16;
76          format.channels = 2;          format.channels = 2;
77          format.rate = 44100;          format.rate = 44100;
78          g_samplerate = 44100;          format.byte_format = AO_FMT_NATIVE;
79          format.byte_format = AO_FMT_LITTLE;  
80    
81          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
82          if (o_device == NULL)          if (o_device == NULL)
# Line 64  wave_out_open(void) Line 84  wave_out_open(void)
84                  return False;                  return False;
85          }          }
86    
87          g_dsp_fd = 0;          reopened = True;
         queue_lo = queue_hi = 0;  
88    
89          return True;          return True;
90  }  }
91    
92  void  void
93  wave_out_close(void)  libao_close(void)
94  {  {
95          /* Ack all remaining packets */          /* Ack all remaining packets */
96          while (queue_lo != queue_hi)          while (!rdpsnd_queue_empty())
97          {          {
98                  rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index);                  rdpsnd_queue_next(0);
                 free(packet_queue[queue_lo].s.data);  
                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
99          }          }
100    
101          if (o_device != NULL)          if (o_device != NULL)
102                  ao_close(o_device);                  ao_close(o_device);
         ao_shutdown();  
 }  
103    
104  BOOL          o_device = NULL;
 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, we need to upsample 22050 to it */  
         if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))  
                 return False;  
105    
106          return True;          ao_shutdown();
107  }  }
108    
109  BOOL  RD_BOOL
110  wave_out_set_format(WAVEFORMATEX * pwfx)  libao_set_format(RD_WAVEFORMATEX * pwfx)
111  {  {
112          ao_sample_format format;          ao_sample_format format;
113    
114          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
115          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
116          format.rate = 44100;          format.rate = 44100;
117          g_samplerate = pwfx->nSamplesPerSec;          format.byte_format = AO_FMT_NATIVE;
         format.byte_format = AO_FMT_LITTLE;  
118    
119          g_samplewidth = pwfx->wBitsPerSample / 8;          if (o_device != NULL)
   
         if(o_device != NULL)  
120                  ao_close(o_device);                  ao_close(o_device);
121    
122          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
# Line 125  wave_out_set_format(WAVEFORMATEX * pwfx) Line 125  wave_out_set_format(WAVEFORMATEX * pwfx)
125                  return False;                  return False;
126          }          }
127    
128            if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
         return True;  
 }  
   
 void  
 wave_out_volume(uint16 left, uint16 right)  
 {  
 }  
   
 void  
 wave_out_write(STREAM s, uint16 tick, uint8 index)  
 {  
         struct audio_packet *packet = &packet_queue[queue_hi];  
         unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;  
   
         if (next_hi == queue_lo)  
129          {          {
130                  error("No space to queue audio packet\n");                  return False;
                 return;  
131          }          }
132    
133          queue_hi = next_hi;          reopened = True;
134    
135          packet->s = *s;          return True;
         packet->tick = tick;  
         packet->index = index;  
         packet->s.p += 4;  
   
         /* we steal the data buffer from s, give it a new one */  
         s->data = malloc(s->size);  
   
         if (!g_dsp_busy)  
                 wave_out_play();  
136  }  }
137    
138  void  void
139  wave_out_play(void)  libao_play(void)
140  {  {
141          struct audio_packet *packet;          struct audio_packet *packet;
142          STREAM out;          STREAM out;
143          unsigned char expanded[WAVEOUTBUF];          int len;
144          int offset,len,i;          static long prev_s, prev_us;
145            unsigned int duration;
146            struct timeval tv;
147            int next_tick;
148    
149            if (reopened)
150            {
151                    reopened = False;
152                    gettimeofday(&tv, NULL);
153                    prev_s = tv.tv_sec;
154                    prev_us = tv.tv_usec;
155            }
156    
157          if (queue_lo == queue_hi)          /* We shouldn't be called if the queue is empty, but still */
158          {          if (rdpsnd_queue_empty())
                 g_dsp_busy = 0;  
159                  return;                  return;
         }  
160    
161          packet = &packet_queue[queue_lo];          packet = rdpsnd_queue_current_packet();
162          out = &packet->s;          out = &packet->s;
163    
164          len = 0;          next_tick = rdpsnd_queue_next_tick();
165    
166            len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
167            ao_play(o_device, (char *) out->p, len);
168            out->p += len;
169    
170            gettimeofday(&tv, NULL);
171    
172          if (g_samplerate == 22050 )          duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
173    
174            if (packet->tick > next_tick)
175                    next_tick += 65536;
176    
177            if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
178          {          {
179                  /* Resample to 44100 */                  unsigned int delay_us;
180                  for(i=0; (i<((WAVEOUTBUF/8)*(3-g_samplewidth))) && (out->p < out->end); i++)  
181                    prev_s = tv.tv_sec;
182                    prev_us = tv.tv_usec;
183    
184                    if (abs((next_tick - packet->tick) - duration) > 20)
185                  {                  {
186                          offset=i*4*g_samplewidth;                          DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
187                          memcpy(&expanded[0*g_samplewidth+offset],out->p,g_samplewidth);                          DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
188                          memcpy(&expanded[2*g_samplewidth+offset],out->p,g_samplewidth);                                 (packet->tick + duration) % 65536, next_tick % 65536));
                         out->p += 2;  
   
                         memcpy(&expanded[1*g_samplewidth+offset],out->p,g_samplewidth);  
                         memcpy(&expanded[3*g_samplewidth+offset],out->p,g_samplewidth);  
                         out->p += 2;  
                         len += 4*g_samplewidth;  
189                  }                  }
190    
191                    delay_us = ((out->size / 4) * (1000000 / 44100));
192    
193                    rdpsnd_queue_next(delay_us);
194          }          }
195          else  }
196          {  
197                  len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF;  struct audio_driver *
198                  memcpy(expanded,out->p,len);  libao_register(char *options)
199                  out->p += len;  {
200          }          static struct audio_driver libao_driver;
201    
202            memset(&libao_driver, 0, sizeof(libao_driver));
203    
204            libao_driver.name = "libao";
205            libao_driver.description = "libao output driver, default device: system dependent";
206    
207            libao_driver.add_fds = libao_add_fds;
208            libao_driver.check_fds = libao_check_fds;
209    
210            libao_driver.wave_out_open = libao_open;
211            libao_driver.wave_out_close = libao_close;
212            libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
213            libao_driver.wave_out_set_format = libao_set_format;
214            libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
215    
216          ao_play(o_device, expanded, len);          libao_driver.need_byteswap_on_be = 1;
217            libao_driver.need_resampling = 1;
218    
219          if (out->p == out->end)          if (options)
220          {          {
221                  rdpsnd_send_completion(packet->tick, packet->index);                  libao_device = xstrdup(options);
                 free(out->data);  
                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
222          }          }
223    
224          g_dsp_busy = 1;          return &libao_driver;
         return;  
225  }  }

Legend:
Removed from v.836  
changed lines
  Added in v.1475

  ViewVC Help
Powered by ViewVC 1.1.26