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

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

revision 801 by astrand, Tue Nov 23 13:29:12 2004 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 - 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>
36    #include <unistd.h>
37  #include <sys/time.h>  #include <sys/time.h>
38  #include <sys/ioctl.h>  #include <sys/ioctl.h>
39  #include <sys/soundcard.h>  #include <sys/soundcard.h>
40    #include <sys/types.h>
41    #include <sys/stat.h>
42    
43    #define DEFAULTDEVICE   "/dev/dsp"
44    #define MAX_LEN         512
45    
46    static int dsp_fd = -1;
47    static BOOL dsp_busy;
48    
49    static int snd_rate;
50    static short samplewidth;
51    static char *dsp_dev;
52    static BOOL in_esddsp;
53    
54  #define MAX_QUEUE       10  /* This is a just a forward declaration */
55    static struct audio_driver oss_driver;
56    
57  int g_dsp_fd;  void oss_play(void);
 BOOL g_dsp_busy = False;  
 static int g_snd_rate;  
 static short g_samplewidth;  
 static BOOL g_driver_broken = False;  
58    
59  static struct audio_packet  void
60    oss_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
61  {  {
62          struct stream s;          if (dsp_fd == -1)
63          uint16 tick;                  return;
         uint8 index;  
 } packet_queue[MAX_QUEUE];  
 static unsigned int queue_hi, queue_lo;  
64    
65  BOOL          if (rdpsnd_queue_empty())
66  wave_out_open(void)                  return;
67    
68            FD_SET(dsp_fd, wfds);
69            if (dsp_fd > *n)
70                    *n = dsp_fd;
71    }
72    
73    void
74    oss_check_fds(fd_set * rfds, fd_set * wfds)
75  {  {
76          char *dsp_dev = getenv("AUDIODEV");          if (FD_ISSET(dsp_fd, wfds))
77                    oss_play();
78    }
79    
80          if (dsp_dev == NULL)  static BOOL
81          {  detect_esddsp(void)
82                  dsp_dev = strdup("/dev/dsp");  {
83          }          struct stat s;
84            char *preload;
85    
86            if (fstat(dsp_fd, &s) == -1)
87                    return False;
88    
89            if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
90                    return False;
91    
92            preload = getenv("LD_PRELOAD");
93            if (preload == NULL)
94                    return False;
95    
96          if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)          if (strstr(preload, "esddsp") == NULL)
97                    return False;
98    
99            return True;
100    }
101    
102    BOOL
103    oss_open(void)
104    {
105            if ((dsp_fd = open(dsp_dev, O_WRONLY)) == -1)
106          {          {
107                  perror(dsp_dev);                  perror(dsp_dev);
108                  return False;                  return False;
109          }          }
110    
111          /* Non-blocking so that user interface is responsive */          in_esddsp = detect_esddsp();
112          fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK);  
113          return True;          return True;
114  }  }
115    
116  void  void
117  wave_out_close(void)  oss_close(void)
118  {  {
119          close(g_dsp_fd);          close(dsp_fd);
120            dsp_fd = -1;
121            dsp_busy = False;
122  }  }
123    
124  BOOL  BOOL
125  wave_out_format_supported(WAVEFORMATEX * pwfx)  oss_format_supported(WAVEFORMATEX * pwfx)
126  {  {
127          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
128                  return False;                  return False;
# Line 84  wave_out_format_supported(WAVEFORMATEX * Line 135  wave_out_format_supported(WAVEFORMATEX *
135  }  }
136    
137  BOOL  BOOL
138  wave_out_set_format(WAVEFORMATEX * pwfx)  oss_set_format(WAVEFORMATEX * pwfx)
139  {  {
140          int stereo, format, fragments;          int stereo, format, fragments;
141            static BOOL driver_broken = False;
142    
143          ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);          ioctl(dsp_fd, SNDCTL_DSP_RESET, NULL);
144          ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);          ioctl(dsp_fd, SNDCTL_DSP_SYNC, NULL);
145    
146          if (pwfx->wBitsPerSample == 8)          if (pwfx->wBitsPerSample == 8)
147                  format = AFMT_U8;                  format = AFMT_U8;
148          else if (pwfx->wBitsPerSample == 16)          else if (pwfx->wBitsPerSample == 16)
149                  format = AFMT_S16_LE;                  format = AFMT_S16_LE;
150    
151          g_samplewidth = pwfx->wBitsPerSample / 8;          samplewidth = pwfx->wBitsPerSample / 8;
152    
153          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)          if (ioctl(dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
154          {          {
155                  perror("SNDCTL_DSP_SETFMT");                  perror("SNDCTL_DSP_SETFMT");
156                  close(g_dsp_fd);                  oss_close();
157                  return False;                  return False;
158          }          }
159    
160          if (pwfx->nChannels == 2)          if (pwfx->nChannels == 2)
161          {          {
162                  stereo = 1;                  stereo = 1;
163                  g_samplewidth *= 2;                  samplewidth *= 2;
164          }          }
165          else          else
166          {          {
167                  stereo = 0;                  stereo = 0;
168          }          }
169    
170          if (ioctl(g_dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1)          if (ioctl(dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1)
171          {          {
172                  perror("SNDCTL_DSP_CHANNELS");                  perror("SNDCTL_DSP_CHANNELS");
173                  close(g_dsp_fd);                  oss_close();
174                  return False;                  return False;
175          }          }
176    
177          g_snd_rate = pwfx->nSamplesPerSec;          oss_driver.need_resampling = 0;
178          if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1)          snd_rate = pwfx->nSamplesPerSec;
179            if (ioctl(dsp_fd, SNDCTL_DSP_SPEED, &snd_rate) == -1)
180          {          {
181                  perror("SNDCTL_DSP_SPEED");                  int rates[] = { 44100, 48000, 0 };
182                  close(g_dsp_fd);                  int *prates = rates;
183                  return False;  
184                    while (*prates != 0)
185                    {
186                            if ((pwfx->nSamplesPerSec != *prates)
187                                && (ioctl(dsp_fd, SNDCTL_DSP_SPEED, prates) != -1))
188                            {
189                                    oss_driver.need_resampling = 1;
190                                    snd_rate = *prates;
191                                    if (rdpsnd_dsp_resample_set
192                                        (snd_rate, pwfx->wBitsPerSample, pwfx->nChannels) == False)
193                                    {
194                                            error("rdpsnd_dsp_resample_set failed");
195                                            oss_close();
196                                            return False;
197                                    }
198    
199                                    break;
200                            }
201                            prates++;
202                    }
203    
204                    if (*prates == 0)
205                    {
206                            perror("SNDCTL_DSP_SPEED");
207                            oss_close();
208                            return False;
209                    }
210          }          }
211    
212          /* try to get 7 fragments of 2^12 bytes size */          /* try to get 12 fragments of 2^12 bytes size */
213          fragments = (7 << 16) + 12;          fragments = (12 << 16) + 12;
214          ioctl(g_dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments);          ioctl(dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments);
215    
216          if (!g_driver_broken)          if (!driver_broken)
217          {          {
218                  audio_buf_info info;                  audio_buf_info info;
219    
220                  memset(&info, 0, sizeof(info));                  memset(&info, 0, sizeof(info));
221                  if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)                  if (ioctl(dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
222                  {                  {
223                          perror("SNDCTL_DSP_GETOSPACE");                          perror("SNDCTL_DSP_GETOSPACE");
224                          close(g_dsp_fd);                          oss_close();
225                          return False;                          return False;
226                  }                  }
227    
# Line 151  wave_out_set_format(WAVEFORMATEX * pwfx) Line 230  wave_out_set_format(WAVEFORMATEX * pwfx)
230                          fprintf(stderr,                          fprintf(stderr,
231                                  "Broken OSS-driver detected: fragments: %d, fragstotal: %d, fragsize: %d\n",                                  "Broken OSS-driver detected: fragments: %d, fragstotal: %d, fragsize: %d\n",
232                                  info.fragments, info.fragstotal, info.fragsize);                                  info.fragments, info.fragstotal, info.fragsize);
233                          g_driver_broken = True;                          driver_broken = True;
234                  }                  }
235          }          }
236    
# Line 159  wave_out_set_format(WAVEFORMATEX * pwfx) Line 238  wave_out_set_format(WAVEFORMATEX * pwfx)
238  }  }
239    
240  void  void
241  wave_out_volume(uint16 left, uint16 right)  oss_volume(uint16 left, uint16 right)
242  {  {
         static BOOL use_dev_mixer = False;  
243          uint32 volume;          uint32 volume;
         int fd_mix = -1;  
244    
245          volume = left / (65536 / 100);          volume = left / (65536 / 100);
246          volume |= right / (65536 / 100) << 8;          volume |= right / (65536 / 100) << 8;
247    
248          if (use_dev_mixer)          if (ioctl(dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
249          {          {
250                  if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1)                  warning("hardware volume control unavailable, falling back to software volume control!\n");
251                  {                  oss_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
252                          perror("open /dev/mixer");                  rdpsnd_dsp_softvol_set(left, right);
                         return;  
                 }  
   
                 if (ioctl(fd_mix, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)  
                 {  
                         perror("MIXER_WRITE(SOUND_MIXER_PCM)");  
                         return;  
                 }  
   
                 close(fd_mix);  
         }  
   
         if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)  
         {  
                 perror("MIXER_WRITE(SOUND_MIXER_PCM)");  
                 use_dev_mixer = True;  
253                  return;                  return;
254          }          }
255  }  }
256    
257  void  void
258  wave_out_write(STREAM s, uint16 tick, uint8 index)  oss_play(void)
259  {  {
260          struct audio_packet *packet = &packet_queue[queue_hi];          struct audio_packet *packet;
261          unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;          ssize_t len;
262            STREAM out;
263    
264          if (next_hi == queue_lo)          /* We shouldn't be called if the queue is empty, but still */
265          {          if (rdpsnd_queue_empty())
                 error("No space to queue audio packet\n");  
266                  return;                  return;
         }  
267    
268          queue_hi = next_hi;          packet = rdpsnd_queue_current_packet();
269            out = &packet->s;
270    
271          packet->s = *s;          len = out->end - out->p;
         packet->tick = tick;  
         packet->index = index;  
         packet->s.p += 4;  
272    
273          /* we steal the data buffer from s, give it a new one */          len = write(dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len);
274          s->data = (uint8 *) malloc(s->size);          if (len == -1)
275            {
276          if (!g_dsp_busy)                  if (errno != EWOULDBLOCK)
277                  wave_out_play();                          perror("write audio");
278  }                  return;
279            }
280    
281  void          out->p += len;
 wave_out_play(void)  
 {  
         struct audio_packet *packet;  
         ssize_t len;  
         STREAM out;  
         static long startedat_us;  
         static long startedat_s;  
         static BOOL started = False;  
         struct timeval tv;  
         audio_buf_info info;  
282    
283          while (1)          if (out->p == out->end)
284          {          {
285                  if (queue_lo == queue_hi)                  int delay_bytes;
286                  {                  unsigned long delay_us;
287                          g_dsp_busy = 0;                  audio_buf_info info;
                         return;  
                 }  
   
                 packet = &packet_queue[queue_lo];  
                 out = &packet->s;  
288    
289                  if (!started)                  if (in_esddsp)
290                  {                  {
291                          gettimeofday(&tv, NULL);                          /* EsounD has no way of querying buffer status, so we have to
292                          startedat_us = tv.tv_usec;                           * go with a fixed size. */
293                          startedat_s = tv.tv_sec;                          delay_bytes = out->size;
                         started = True;  
294                  }                  }
295                    else
                 len = out->end - out->p;  
   
                 if (!g_driver_broken)  
296                  {                  {
297                          memset(&info, 0, sizeof(info));  #ifdef SNDCTL_DSP_GETODELAY
298                          if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)                          delay_bytes = 0;
299                          {                          if (ioctl(dsp_fd, SNDCTL_DSP_GETODELAY, &delay_bytes) == -1)
300                                  perror("SNDCTL_DSP_GETOSPACE");                                  delay_bytes = -1;
301                                  return;  #else
302                          }                          delay_bytes = -1;
303    #endif
                         if (info.fragments == 0)  
                         {  
                                 g_dsp_busy = 1;  
                                 return;  
                         }  
304    
305                          if (info.fragments * info.fragsize < len                          if (delay_bytes == -1)
                             && info.fragments * info.fragsize > 0)  
306                          {                          {
307                                  len = info.fragments * info.fragsize;                                  if (ioctl(dsp_fd, SNDCTL_DSP_GETOSPACE, &info) != -1)
308                                            delay_bytes = info.fragstotal * info.fragsize - info.bytes;
309                                    else
310                                            delay_bytes = out->size;
311                          }                          }
312                  }                  }
313    
314                    delay_us = delay_bytes * (1000000 / (samplewidth * snd_rate));
315                    rdpsnd_queue_next(delay_us);
316            }
317    }
318    
319                  len = write(g_dsp_fd, out->p, len);  static struct audio_driver oss_driver = {
320                  if (len == -1)          .name = "oss",
321                  {          .description = "OSS output driver, default device: " DEFAULTDEVICE " or $AUDIODEV",
322                          if (errno != EWOULDBLOCK)  
323                                  perror("write audio");          .add_fds = oss_add_fds,
324                          g_dsp_busy = 1;          .check_fds = oss_check_fds,
325                          return;  
326                  }          .wave_out_open = oss_open,
327            .wave_out_close = oss_close,
328                  out->p += len;          .wave_out_format_supported = oss_format_supported,
329                  if (out->p == out->end)          .wave_out_set_format = oss_set_format,
330                  {          .wave_out_volume = oss_volume,
331                          long long duration;  
332                          long elapsed;          .need_byteswap_on_be = 0,
333            .need_resampling = 0,
334    };
335    
336                          gettimeofday(&tv, NULL);  struct audio_driver *
337                          duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate)));  oss_register(char *options)
338                          elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);  {
339            if (options)
340            {
341                    dsp_dev = xstrdup(options);
342            }
343            else
344            {
345                    dsp_dev = getenv("AUDIODEV");
346    
347                          if (elapsed >= (duration * 85) / 100)                  if (dsp_dev == NULL)
348                          {                  {
349                                  rdpsnd_send_completion(packet->tick, packet->index);                          dsp_dev = xstrdup(DEFAULTDEVICE);
                                 free(out->data);  
                                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
                                 started = False;  
                         }  
                         else  
                         {  
                                 g_dsp_busy = 1;  
                                 return;  
                         }  
350                  }                  }
351          }          }
352    
353            return &oss_driver;
354  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26