/[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 838 by stargo, Tue Mar 8 12:29:19 2005 UTC revision 1260 by stargo, Sun Sep 17 15:25:10 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      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 int samplerate;
37  int g_samplerate;  static int audiochannels;
38  BOOL g_dsp_busy = False;  static BOOL reopened;
39  static short g_samplewidth;  static short samplewidth;
40    static char *libao_device = NULL;
 static struct audio_packet  
 {  
         struct stream s;  
         uint16 tick;  
         uint8 index;  
 } packet_queue[MAX_QUEUE];  
 static unsigned int queue_hi, queue_lo;  
41    
42  BOOL  BOOL
43  wave_out_open(void)  libao_open(void)
44  {  {
45          ao_sample_format format;          ao_sample_format format;
46            static int warned = 0;
47    
48            if (!warned && libao_device)
49            {
50                    warning("device-options not supported for libao-driver\n");
51                    warned = 1;
52            }
53    
54          ao_initialize();          ao_initialize();
55          default_driver = ao_default_driver_id();          default_driver = ao_default_driver_id();
56    
57          format.bits = 16;          format.bits = 16;
58          format.channels = 2;          format.channels = 2;
59            audiochannels = 2;
60          format.rate = 44100;          format.rate = 44100;
61          g_samplerate = 44100;          samplerate = 44100;
62          format.byte_format = AO_FMT_LITTLE;          format.byte_format = AO_FMT_LITTLE;
63    
64          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
# Line 65  wave_out_open(void) Line 68  wave_out_open(void)
68          }          }
69    
70          g_dsp_fd = 0;          g_dsp_fd = 0;
71          queue_lo = queue_hi = 0;          rdpsnd_queue_init();
72    
73            reopened = True;
74    
75          return True;          return True;
76  }  }
77    
78  void  void
79  wave_out_close(void)  libao_close(void)
80  {  {
81          /* Ack all remaining packets */          /* Ack all remaining packets */
82          while (queue_lo != queue_hi)          while (!rdpsnd_queue_empty())
83          {          {
84                  rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index);                  rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,
85                  free(packet_queue[queue_lo].s.data);                                         rdpsnd_queue_current_packet()->index);
86                  queue_lo = (queue_lo + 1) % MAX_QUEUE;                  rdpsnd_queue_next();
87          }          }
88    
89          if (o_device != NULL)          if (o_device != NULL)
# Line 88  wave_out_close(void) Line 93  wave_out_close(void)
93  }  }
94    
95  BOOL  BOOL
96  wave_out_format_supported(WAVEFORMATEX * pwfx)  libao_format_supported(WAVEFORMATEX * pwfx)
97  {  {
98          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
99                  return False;                  return False;
# Line 105  wave_out_format_supported(WAVEFORMATEX * Line 110  wave_out_format_supported(WAVEFORMATEX *
110  }  }
111    
112  BOOL  BOOL
113  wave_out_set_format(WAVEFORMATEX * pwfx)  libao_set_format(WAVEFORMATEX * pwfx)
114  {  {
115          ao_sample_format format;          ao_sample_format format;
116    
117          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
118          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
119            audiochannels = pwfx->nChannels;
120          format.rate = 44100;          format.rate = 44100;
121          g_samplerate = pwfx->nSamplesPerSec;          samplerate = pwfx->nSamplesPerSec;
122          format.byte_format = AO_FMT_LITTLE;          format.byte_format = AO_FMT_LITTLE;
123    
124          g_samplewidth = pwfx->wBitsPerSample / 8;          samplewidth = pwfx->wBitsPerSample / 8;
125    
126          if (o_device != NULL)          if (o_device != NULL)
127                  ao_close(o_device);                  ao_close(o_device);
# Line 126  wave_out_set_format(WAVEFORMATEX * pwfx) Line 132  wave_out_set_format(WAVEFORMATEX * pwfx)
132                  return False;                  return False;
133          }          }
134    
135            reopened = True;
136    
137          return True;          return True;
138  }  }
139    
140  void  void
141  wave_out_volume(uint16 left, uint16 right)  libao_play(void)
 {  
         warning("volume changes not supported with libao-output\n");  
 }  
   
 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)  
         {  
                 error("No space to queue audio packet\n");  
                 return;  
         }  
   
         queue_hi = next_hi;  
   
         packet->s = *s;  
         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();  
 }  
   
 void  
 wave_out_play(void)  
142  {  {
143          struct audio_packet *packet;          struct audio_packet *packet;
144          STREAM out;          STREAM out;
145          unsigned char outbuf[WAVEOUTBUF];          char outbuf[WAVEOUTBUF];
146          int offset, len, i;          int offset, len, i;
147            static long prev_s, prev_us;
148            unsigned int duration;
149            struct timeval tv;
150            int next_tick;
151    
152          if (queue_lo == queue_hi)          if (reopened)
153            {
154                    reopened = False;
155                    gettimeofday(&tv, NULL);
156                    prev_s = tv.tv_sec;
157                    prev_us = tv.tv_usec;
158            }
159    
160            if (rdpsnd_queue_empty())
161          {          {
162                  g_dsp_busy = 0;                  g_dsp_busy = 0;
163                  return;                  return;
164          }          }
165    
166          packet = &packet_queue[queue_lo];          packet = rdpsnd_queue_current_packet();
167          out = &packet->s;          out = &packet->s;
168    
169            next_tick = rdpsnd_queue_next_tick();
170    
171          len = 0;          len = 0;
172    
173          if (g_samplerate == 22050)          if (samplerate == 22050)
174          {          {
175                  /* Resample to 44100 */                  /* Resample to 44100 */
176                  for (i = 0; (i < ((WAVEOUTBUF / 8) * (3 - g_samplewidth))) && (out->p < out->end);                  for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - samplewidth))) && (out->p < out->end);
177                       i++)                       i++)
178                  {                  {
179                          offset = i * 4 * g_samplewidth;                          /* On a stereo-channel we must make sure that left and right
180                          memcpy(&outbuf[0 * g_samplewidth + offset], out->p, g_samplewidth);                             does not get mixed up, so we need to expand the sample-
181                          memcpy(&outbuf[2 * g_samplewidth + offset], out->p, g_samplewidth);                             data with channels in mind: 1234 -> 12123434
182                          out->p += 2;                             If we have a mono-channel, we can expand the data by simply
183                               doubling the sample-data: 1234 -> 11223344 */
184                          memcpy(&outbuf[1 * g_samplewidth + offset], out->p, g_samplewidth);                          if (audiochannels == 2)
185                          memcpy(&outbuf[3 * g_samplewidth + offset], out->p, g_samplewidth);                                  offset = ((i * 2) - (i & 1)) * samplewidth;
186                          out->p += 2;                          else
187                          len += 4 * g_samplewidth;                                  offset = (i * 2) * samplewidth;
188    
189                            memcpy(&outbuf[offset], out->p, samplewidth);
190                            memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth);
191    
192                            out->p += samplewidth;
193                            len += 2 * samplewidth;
194                  }                  }
195          }          }
196          else          else
# Line 207  wave_out_play(void) Line 202  wave_out_play(void)
202    
203          ao_play(o_device, outbuf, len);          ao_play(o_device, outbuf, len);
204    
205          if (out->p == out->end)          gettimeofday(&tv, NULL);
206    
207            duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
208    
209            if (packet->tick > next_tick)
210                    next_tick += 65536;
211    
212            if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
213          {          {
214                  rdpsnd_send_completion(packet->tick, packet->index);                  prev_s = tv.tv_sec;
215                  free(out->data);                  prev_us = tv.tv_usec;
216                  queue_lo = (queue_lo + 1) % MAX_QUEUE;  
217                    if (abs((next_tick - packet->tick) - duration) > 20)
218                    {
219                            DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
220                            DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
221                                   (packet->tick + duration) % 65536, next_tick % 65536));
222                    }
223    
224                    rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);
225                    rdpsnd_queue_next();
226          }          }
227    
228          g_dsp_busy = 1;          g_dsp_busy = 1;
229          return;          return;
230  }  }
231    
232    struct audio_driver *
233    libao_register(char *options)
234    {
235            static struct audio_driver libao_driver;
236    
237            libao_driver.wave_out_write = rdpsnd_queue_write;
238            libao_driver.wave_out_open = libao_open;
239            libao_driver.wave_out_close = libao_close;
240            libao_driver.wave_out_format_supported = libao_format_supported;
241            libao_driver.wave_out_set_format = libao_set_format;
242            libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
243            libao_driver.wave_out_play = libao_play;
244            libao_driver.name = xstrdup("libao");
245            libao_driver.description = xstrdup("libao output driver");
246            libao_driver.need_byteswap_on_be = 0;
247            libao_driver.next = NULL;
248    
249            if (options)
250            {
251                    libao_device = xstrdup(options);
252            }
253    
254            return &libao_driver;
255    }

Legend:
Removed from v.838  
changed lines
  Added in v.1260

  ViewVC Help
Powered by ViewVC 1.1.26