/[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 1346 by ossman_, Thu Dec 7 15:23:45 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 WAVEOUTLEN      16
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;
38  static short g_samplewidth;  
39    void libao_play(void);
40  static struct audio_packet  
41  {  void
42          struct stream s;  libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
43          uint16 tick;  {
44          uint8 index;  }
45  } packet_queue[MAX_QUEUE];  
46  static unsigned int queue_hi, queue_lo;  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  wave_out_open(void)  libao_open(void)
58  {  {
59          ao_sample_format format;          ao_sample_format format;
60    
61          ao_initialize();          ao_initialize();
62          default_driver = ao_default_driver_id();  
63            if (libao_device)
64            {
65                    default_driver = ao_driver_id(libao_device);
66            }
67            else
68            {
69                    default_driver = ao_default_driver_id();
70            }
71    
72          format.bits = 16;          format.bits = 16;
73          format.channels = 2;          format.channels = 2;
74          format.rate = 44100;          format.rate = 44100;
75          format.byte_format = AO_FMT_LITTLE;          format.byte_format = AO_FMT_NATIVE;
76    
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 61  wave_out_open(void) Line 81  wave_out_open(void)
81                  return False;                  return False;
82          }          }
83    
84          g_dsp_fd = 0;          reopened = True;
         queue_lo = queue_hi = 0;  
85    
86          return True;          return True;
87  }  }
88    
89  void  void
90  wave_out_close(void)  libao_close(void)
91  {  {
92          /* Ack all remaining packets */          /* Ack all remaining packets */
93          while (queue_lo != queue_hi)          while (!rdpsnd_queue_empty())
94          {          {
95                  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;  
96          }          }
97    
98          if (o_device != NULL)          if (o_device != NULL)
99                  ao_close(o_device);                  ao_close(o_device);
         ao_shutdown();  
 }  
100    
101  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, windows gives a max of 22050. we need to upsample that...  
            TODO: support 11025, too */  
         if (pwfx->nSamplesPerSec != 22050)  
                 return False;  
102    
103          return True;          ao_shutdown();
104  }  }
105    
106  BOOL  BOOL
107  wave_out_set_format(WAVEFORMATEX * pwfx)  libao_set_format(WAVEFORMATEX * pwfx)
108  {  {
109          ao_sample_format format;          ao_sample_format format;
110    
         printf("%d\n",pwfx->wBitsPerSample);  
111          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
112          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
113          format.rate = 44100;          format.rate = 44100;
114          format.byte_format = AO_FMT_LITTLE;          format.byte_format = AO_FMT_NATIVE;
   
         g_samplewidth = pwfx->wBitsPerSample / 8;  
115    
116          if(o_device != NULL)          if (o_device != NULL)
117                  ao_close(o_device);                  ao_close(o_device);
118    
119          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 122  wave_out_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          return True;          reopened = True;
 }  
131    
132  void          return True;
 wave_out_volume(uint16 left, uint16 right)  
 {  
133  }  }
134    
135  void  void
136  wave_out_write(STREAM s, uint16 tick, uint8 index)  libao_play(void)
137  {  {
138          struct audio_packet *packet = &packet_queue[queue_hi];          struct audio_packet *packet;
139          unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;          STREAM out;
140            int len;
141            static long prev_s, prev_us;
142            unsigned int duration;
143            struct timeval tv;
144            int next_tick;
145    
146          if (next_hi == queue_lo)          if (reopened)
147          {          {
148                  error("No space to queue audio packet\n");                  reopened = False;
149                  return;                  gettimeofday(&tv, NULL);
150                    prev_s = tv.tv_sec;
151                    prev_us = tv.tv_usec;
152          }          }
153    
154          queue_hi = next_hi;          /* We shouldn't be called if the queue is empty, but still */
155            if (rdpsnd_queue_empty())
156                    return;
157    
158          packet->s = *s;          packet = rdpsnd_queue_current_packet();
159          packet->tick = tick;          out = &packet->s;
         packet->index = index;  
         packet->s.p += 4;  
160    
161          /* we steal the data buffer from s, give it a new one */          next_tick = rdpsnd_queue_next_tick();
         s->data = malloc(s->size);  
162    
163          if (!g_dsp_busy)          len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
164                  wave_out_play();          ao_play(o_device, (char *) out->p, len);
165  }          out->p += len;
166    
167  void          gettimeofday(&tv, NULL);
168  wave_out_play(void)  
169  {          duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
         struct audio_packet *packet;  
         STREAM out;  
         unsigned char expanded[8];  
170    
171          while (1)          if (packet->tick > next_tick)
172                    next_tick += 65536;
173    
174            if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
175          {          {
176                  if (queue_lo == queue_hi)                  prev_s = tv.tv_sec;
177                    prev_us = tv.tv_usec;
178    
179                    if (abs((next_tick - packet->tick) - duration) > 20)
180                  {                  {
181                          g_dsp_busy = 0;                          DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
182                          return;                          DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
183                                   (packet->tick + duration) % 65536, next_tick % 65536));
184                  }                  }
185    
186                  packet = &packet_queue[queue_lo];                  rdpsnd_queue_next(duration);
187                  out = &packet->s;          }
188    }
                 /* Resample 22050 -> 44100 */  
                 /* 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;  
189    
190                  ao_play(o_device, expanded, g_samplewidth*4);  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                  if (out->p == out->end)  struct audio_driver *
208                  {  libao_register(char *options)
209                          rdpsnd_send_completion(packet->tick, packet->index);  {
210                          free(out->data);          if (options)
211                          queue_lo = (queue_lo + 1) % MAX_QUEUE;          {
212                  } else {                  libao_device = xstrdup(options);
                         g_dsp_busy = 1;  
                         return;  
                 }  
213          }          }
214    
215            return &libao_driver;
216  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26