/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_oss.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_oss.c

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

revision 760 by stargo, Fri Sep 3 18:04:48 2004 UTC revision 1269 by stargo, Mon Sep 18 10:17:24 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 - Open Sound System     Sound Channel Process Functions - Open Sound System
4     Copyright (C) Matthew Chapman 2003     Copyright (C) Matthew Chapman 2003
# Line 19  Line 19 
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */  */
21    
22    /*
23       This is a workaround for Esound bug 312665.
24       FIXME: Remove this when Esound is fixed.
25    */
26    #ifdef _FILE_OFFSET_BITS
27    #undef _FILE_OFFSET_BITS
28    #endif
29    
30  #include "rdesktop.h"  #include "rdesktop.h"
31    #include "rdpsnd.h"
32    #include "rdpsnd_dsp.h"
33  #include <unistd.h>  #include <unistd.h>
34  #include <fcntl.h>  #include <fcntl.h>
35  #include <errno.h>  #include <errno.h>
# Line 27  Line 37 
37  #include <sys/ioctl.h>  #include <sys/ioctl.h>
38  #include <sys/soundcard.h>  #include <sys/soundcard.h>
39    
40  #define MAX_QUEUE       10  #define DEFAULTDEVICE   "/dev/dsp"
41    #define MAX_LEN         512
42    
43  int g_dsp_fd;  static int snd_rate;
44  BOOL g_dsp_busy = False;  static short samplewidth;
45  static int g_snd_rate;  static char *dsp_dev;
46  static short g_samplewidth;  static struct audio_driver oss_driver;
   
 static struct audio_packet  
 {  
         struct stream s;  
         uint16 tick;  
         uint8 index;  
 } packet_queue[MAX_QUEUE];  
 static unsigned int queue_hi, queue_lo;  
47    
48  BOOL  BOOL
49  wave_out_open(void)  oss_open(void)
50  {  {
51          char *dsp_dev = getenv("AUDIODEV");          if ((g_dsp_fd = open(dsp_dev, O_WRONLY)) == -1)
   
         if (dsp_dev == NULL)  
         {  
                 dsp_dev = "/dev/dsp";  
         }  
   
         if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)  
52          {          {
53                  perror(dsp_dev);                  perror(dsp_dev);
54                  return False;                  return False;
55          }          }
56    
         /* Non-blocking so that user interface is responsive */  
         fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK);  
57          return True;          return True;
58  }  }
59    
60  void  void
61  wave_out_close(void)  oss_close(void)
62  {  {
63          close(g_dsp_fd);          close(g_dsp_fd);
64  }  }
65    
66  BOOL  BOOL
67  wave_out_format_supported(WAVEFORMATEX * pwfx)  oss_format_supported(WAVEFORMATEX * pwfx)
68  {  {
69          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
70                  return False;                  return False;
# Line 83  wave_out_format_supported(WAVEFORMATEX * Line 77  wave_out_format_supported(WAVEFORMATEX *
77  }  }
78    
79  BOOL  BOOL
80  wave_out_set_format(WAVEFORMATEX * pwfx)  oss_set_format(WAVEFORMATEX * pwfx)
81  {  {
82          int stereo, format;          int stereo, format, fragments;
83            static BOOL driver_broken = False;
84    
85          ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);          ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);
86          ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);          ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);
# Line 95  wave_out_set_format(WAVEFORMATEX * pwfx) Line 90  wave_out_set_format(WAVEFORMATEX * pwfx)
90          else if (pwfx->wBitsPerSample == 16)          else if (pwfx->wBitsPerSample == 16)
91                  format = AFMT_S16_LE;                  format = AFMT_S16_LE;
92    
93          g_samplewidth = pwfx->wBitsPerSample / 8;          samplewidth = pwfx->wBitsPerSample / 8;
94    
95          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
96          {          {
# Line 107  wave_out_set_format(WAVEFORMATEX * pwfx) Line 102  wave_out_set_format(WAVEFORMATEX * pwfx)
102          if (pwfx->nChannels == 2)          if (pwfx->nChannels == 2)
103          {          {
104                  stereo = 1;                  stereo = 1;
105                  g_samplewidth *= 2;                  samplewidth *= 2;
106          }          }
107          else          else
108          {          {
# Line 121  wave_out_set_format(WAVEFORMATEX * pwfx) Line 116  wave_out_set_format(WAVEFORMATEX * pwfx)
116                  return False;                  return False;
117          }          }
118    
119          g_snd_rate = pwfx->nSamplesPerSec;          snd_rate = pwfx->nSamplesPerSec;
120          if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1)          if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &snd_rate) == -1)
121          {          {
122                  perror("SNDCTL_DSP_SPEED");                  perror("SNDCTL_DSP_SPEED");
123                  close(g_dsp_fd);                  close(g_dsp_fd);
124                  return False;                  return False;
125          }          }
126    
127          return True;          /* try to get 12 fragments of 2^12 bytes size */
128  }          fragments = (12 << 16) + 12;
129            ioctl(g_dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments);
 void  
 wave_out_volume(uint16 left, uint16 right)  
 {  
         static BOOL use_dev_mixer = False;  
         uint32 volume;  
         int fd_mix = -1;  
   
         volume = left / (65536 / 100);  
         volume |= right / (65536 / 100) << 8;  
130    
131          if (use_dev_mixer)          if (!driver_broken)
132          {          {
133                  if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1)                  audio_buf_info info;
134    
135                    memset(&info, 0, sizeof(info));
136                    if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
137                  {                  {
138                          perror("open /dev/mixer");                          perror("SNDCTL_DSP_GETOSPACE");
139                          return;                          close(g_dsp_fd);
140                            return False;
141                  }                  }
142    
143                  if (ioctl(fd_mix, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)                  if (info.fragments == 0 || info.fragstotal == 0 || info.fragsize == 0)
144                  {                  {
145                          perror("MIXER_WRITE(SOUND_MIXER_PCM)");                          fprintf(stderr,
146                          return;                                  "Broken OSS-driver detected: fragments: %d, fragstotal: %d, fragsize: %d\n",
147                                    info.fragments, info.fragstotal, info.fragsize);
148                            driver_broken = True;
149                  }                  }
   
                 close(fd_mix);  
150          }          }
151    
152          if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)          return True;
         {  
                 perror("MIXER_WRITE(SOUND_MIXER_PCM)");  
                 use_dev_mixer = True;  
                 return;  
         }  
153  }  }
154    
155  void  void
156  wave_out_write(STREAM s, uint16 tick, uint8 index)  oss_volume(uint16 left, uint16 right)
157  {  {
158          struct audio_packet *packet = &packet_queue[queue_hi];          uint32 volume;
         unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;  
159    
160          if (next_hi == queue_lo)          volume = left / (65536 / 100);
161            volume |= right / (65536 / 100) << 8;
162    
163            if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
164          {          {
165                  error("No space to queue audio packet\n");                  perror("MIXER_WRITE(SOUND_MIXER_PCM)");
166                    oss_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
167                  return;                  return;
168          }          }
   
         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();  
169  }  }
170    
171  void  void
172  wave_out_play(void)  oss_play(void)
173  {  {
174          struct audio_packet *packet;          struct audio_packet *packet;
175          ssize_t len;          ssize_t len;
# Line 204  wave_out_play(void) Line 179  wave_out_play(void)
179          static BOOL started = False;          static BOOL started = False;
180          struct timeval tv;          struct timeval tv;
181    
182          while (1)          if (rdpsnd_queue_empty())
183          {          {
184                  if (queue_lo == queue_hi)                  g_dsp_busy = 0;
185                  {                  return;
186                          g_dsp_busy = 0;          }
187                          return;  
188                  }          packet = rdpsnd_queue_current_packet();
189            out = &packet->s;
190    
191            if (!started)
192            {
193                    gettimeofday(&tv, NULL);
194                    startedat_us = tv.tv_usec;
195                    startedat_s = tv.tv_sec;
196                    started = True;
197            }
198    
199                  packet = &packet_queue[queue_lo];          len = out->end - out->p;
                 out = &packet->s;  
200    
201                  if (!started)          len = write(g_dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len);
202            if (len == -1)
203            {
204                    if (errno != EWOULDBLOCK)
205                            perror("write audio");
206                    g_dsp_busy = 1;
207                    return;
208            }
209    
210            out->p += len;
211            if (out->p == out->end)
212            {
213                    long long duration;
214                    long elapsed;
215    
216                    gettimeofday(&tv, NULL);
217                    duration = (out->size * (1000000 / (samplewidth * snd_rate)));
218                    elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);
219    
220                    if (elapsed >= (duration * 85) / 100)
221                  {                  {
222                          gettimeofday(&tv, NULL);                          /* We need to add 50 to tell windows that time has passed while
223                          startedat_us = tv.tv_usec;                           * playing this packet */
224                          startedat_s = tv.tv_sec;                          rdpsnd_send_completion(packet->tick + 50, packet->index);
225                          started = True;                          rdpsnd_queue_next();
226                            started = False;
227                  }                  }
228                    else
                 len = write(g_dsp_fd, out->p, out->end - out->p);  
                 if (len == -1)  
229                  {                  {
                         if (errno != EWOULDBLOCK)  
                                 perror("write audio");  
230                          g_dsp_busy = 1;                          g_dsp_busy = 1;
231                          return;                          return;
232                  }                  }
233            }
234            g_dsp_busy = 1;
235            return;
236    }
237    
238                  out->p += len;  struct audio_driver *
239                  if (out->p == out->end)  oss_register(char *options)
240                  {  {
241                          long long duration;          oss_driver.wave_out_write = rdpsnd_queue_write;
242                          long elapsed;          oss_driver.wave_out_open = oss_open;
243            oss_driver.wave_out_close = oss_close;
244            oss_driver.wave_out_format_supported = oss_format_supported;
245            oss_driver.wave_out_set_format = oss_set_format;
246            oss_driver.wave_out_volume = oss_volume;
247            oss_driver.wave_out_play = oss_play;
248            oss_driver.name = xstrdup("oss");
249            oss_driver.description =
250                    xstrdup("OSS output driver, default device: " DEFAULTDEVICE " or $AUDIODEV");
251            oss_driver.need_byteswap_on_be = 0;
252            oss_driver.next = NULL;
253    
254            if (options)
255            {
256                    dsp_dev = xstrdup(options);
257            }
258            else
259            {
260                    dsp_dev = getenv("AUDIODEV");
261    
262                          gettimeofday(&tv, NULL);                  if (dsp_dev == NULL)
263                          duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate)));                  {
264                          elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);                          dsp_dev = xstrdup(DEFAULTDEVICE);
   
                         if (elapsed >= (duration * 85) / 100)  
                         {  
                                 rdpsnd_send_completion(packet->tick, packet->index);  
                                 free(out->data);  
                                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
                                 started = False;  
                         }  
                         else  
                         {  
                                 g_dsp_busy = 1;  
                                 return;  
                         }  
265                  }                  }
266          }          }
267    
268            return &oss_driver;
269  }  }

Legend:
Removed from v.760  
changed lines
  Added in v.1269

  ViewVC Help
Powered by ViewVC 1.1.26