/[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 1254 by stargo, Sun Sep 17 10:32:18 2006 UTC revision 1365 by jsorg71, Thu Jan 4 05:39:39 2007 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- 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-2007
5     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6       Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
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 29  Line 30 
30    
31  #include "rdesktop.h"  #include "rdesktop.h"
32  #include "rdpsnd.h"  #include "rdpsnd.h"
33    #include "rdpsnd_dsp.h"
34  #include <unistd.h>  #include <unistd.h>
35  #include <fcntl.h>  #include <fcntl.h>
36  #include <errno.h>  #include <errno.h>
37    #include <unistd.h>
38  #include <sys/time.h>  #include <sys/time.h>
39  #include <sys/ioctl.h>  #include <sys/ioctl.h>
40  #include <sys/soundcard.h>  #include <sys/soundcard.h>
41    #include <sys/types.h>
42    #include <sys/stat.h>
43    
44  #define DEFAULTDEVICE   "/dev/dsp"  #define DEFAULTDEVICE   "/dev/dsp"
45  #define MAX_LEN         512  #define MAX_LEN         512
46    
47    static int dsp_fd = -1;
48    static int dsp_mode;
49    static int dsp_refs;
50    
51    static BOOL dsp_configured;
52    
53    static BOOL dsp_out;
54    static BOOL dsp_in;
55    
56    static int stereo;
57    static int format;
58  static int snd_rate;  static int snd_rate;
59  static short samplewidth;  static short samplewidth;
60    static char *dsp_dev;
61    static BOOL in_esddsp;
62    
63    /* This is a just a forward declaration */
64    static struct audio_driver oss_driver;
65    
66    void oss_play(void);
67    void oss_record(void);
68    
69    void
70    oss_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
71    {
72            if (dsp_fd == -1)
73                    return;
74    
75            if (dsp_out && !rdpsnd_queue_empty())
76                    FD_SET(dsp_fd, wfds);
77            if (dsp_in)
78                    FD_SET(dsp_fd, rfds);
79            if (dsp_fd > *n)
80                    *n = dsp_fd;
81    }
82    
83    void
84    oss_check_fds(fd_set * rfds, fd_set * wfds)
85    {
86            if (FD_ISSET(dsp_fd, wfds))
87                    oss_play();
88            if (FD_ISSET(dsp_fd, rfds))
89                    oss_record();
90    }
91    
92    static BOOL
93    detect_esddsp(void)
94    {
95            struct stat s;
96            char *preload;
97    
98            if (fstat(dsp_fd, &s) == -1)
99                    return False;
100    
101            if (S_ISCHR(s.st_mode) || S_ISBLK(s.st_mode))
102                    return False;
103    
104            preload = getenv("LD_PRELOAD");
105            if (preload == NULL)
106                    return False;
107    
108            if (strstr(preload, "esddsp") == NULL)
109                    return False;
110    
111            return True;
112    }
113    
114  BOOL  BOOL
115  wave_out_open(void)  oss_open(int fallback)
116  {  {
117          char *dsp_dev = getenv("AUDIODEV");          int caps;
118    
119          if (dsp_dev == NULL)          if (dsp_fd != -1)
120          {          {
121                  dsp_dev = xstrdup(DEFAULTDEVICE);                  dsp_refs++;
122    
123                    if (dsp_mode == O_RDWR)
124                            return True;
125    
126                    if (dsp_mode == fallback)
127                            return True;
128    
129                    dsp_refs--;
130                    return False;
131          }          }
132    
133          if ((g_dsp_fd = open(dsp_dev, O_WRONLY)) == -1)          dsp_configured = False;
134    
135            dsp_mode = O_RDWR;
136            dsp_fd = open(dsp_dev, O_RDWR | O_NONBLOCK);
137            if (dsp_fd != -1)
138          {          {
139                  perror(dsp_dev);                  ioctl(dsp_fd, SNDCTL_DSP_SETDUPLEX, 0);
140                  return False;  
141                    if ((ioctl(dsp_fd, SNDCTL_DSP_GETCAPS, &caps) < 0) || !(caps & DSP_CAP_DUPLEX))
142                    {
143                            close(dsp_fd);
144                            dsp_fd = -1;
145                    }
146          }          }
147    
148            if (dsp_fd == -1)
149            {
150                    dsp_mode = fallback;
151    
152                    dsp_fd = open(dsp_dev, dsp_mode | O_NONBLOCK);
153                    if (dsp_fd == -1)
154                    {
155                            perror(dsp_dev);
156                            return False;
157                    }
158            }
159    
160            dsp_refs++;
161    
162            in_esddsp = detect_esddsp();
163    
164            return True;
165    }
166    
167    void
168    oss_close(void)
169    {
170            dsp_refs--;
171    
172            if (dsp_refs != 0)
173                    return;
174    
175            close(dsp_fd);
176            dsp_fd = -1;
177    }
178    
179    BOOL
180    oss_open_out(void)
181    {
182            if (!oss_open(O_WRONLY))
183                    return False;
184    
185            dsp_out = True;
186    
187          return True;          return True;
188  }  }
189    
190  void  void
191  wave_out_close(void)  oss_close_out(void)
192  {  {
193          close(g_dsp_fd);          oss_close();
194    
195            /* Ack all remaining packets */
196            while (!rdpsnd_queue_empty())
197                    rdpsnd_queue_next(0);
198    
199            dsp_out = False;
200  }  }
201    
202  BOOL  BOOL
203  wave_out_format_supported(WAVEFORMATEX * pwfx)  oss_open_in(void)
204    {
205            if (!oss_open(O_RDONLY))
206                    return False;
207    
208            dsp_in = True;
209    
210            return True;
211    }
212    
213    void
214    oss_close_in(void)
215    {
216            oss_close();
217    
218            dsp_in = False;
219    }
220    
221    BOOL
222    oss_format_supported(RD_WAVEFORMATEX * pwfx)
223  {  {
224          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
225                  return False;                  return False;
# Line 81  wave_out_format_supported(WAVEFORMATEX * Line 232  wave_out_format_supported(WAVEFORMATEX *
232  }  }
233    
234  BOOL  BOOL
235  wave_out_set_format(WAVEFORMATEX * pwfx)  oss_set_format(RD_WAVEFORMATEX * pwfx)
236  {  {
237          int stereo, format, fragments;          int fragments;
238          static BOOL driver_broken = False;          static BOOL driver_broken = False;
239    
240          ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);          if (dsp_configured)
241          ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);          {
242                    if ((pwfx->wBitsPerSample == 8) && (format != AFMT_U8))
243                            return False;
244                    if ((pwfx->wBitsPerSample == 16) && (format != AFMT_S16_LE))
245                            return False;
246    
247                    if ((pwfx->nChannels == 2) != !!stereo)
248                            return False;
249    
250                    if (pwfx->nSamplesPerSec != snd_rate)
251                            return False;
252    
253                    return True;
254            }
255    
256            ioctl(dsp_fd, SNDCTL_DSP_RESET, NULL);
257            ioctl(dsp_fd, SNDCTL_DSP_SYNC, NULL);
258    
259          if (pwfx->wBitsPerSample == 8)          if (pwfx->wBitsPerSample == 8)
260                  format = AFMT_U8;                  format = AFMT_U8;
# Line 96  wave_out_set_format(WAVEFORMATEX * pwfx) Line 263  wave_out_set_format(WAVEFORMATEX * pwfx)
263    
264          samplewidth = pwfx->wBitsPerSample / 8;          samplewidth = pwfx->wBitsPerSample / 8;
265    
266          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)          if (ioctl(dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
267          {          {
268                  perror("SNDCTL_DSP_SETFMT");                  perror("SNDCTL_DSP_SETFMT");
269                  close(g_dsp_fd);                  oss_close();
270                  return False;                  return False;
271          }          }
272    
# Line 113  wave_out_set_format(WAVEFORMATEX * pwfx) Line 280  wave_out_set_format(WAVEFORMATEX * pwfx)
280                  stereo = 0;                  stereo = 0;
281          }          }
282    
283          if (ioctl(g_dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1)          if (ioctl(dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1)
284          {          {
285                  perror("SNDCTL_DSP_CHANNELS");                  perror("SNDCTL_DSP_CHANNELS");
286                  close(g_dsp_fd);                  oss_close();
287                  return False;                  return False;
288          }          }
289    
290            oss_driver.need_resampling = 0;
291          snd_rate = pwfx->nSamplesPerSec;          snd_rate = pwfx->nSamplesPerSec;
292          if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &snd_rate) == -1)          if (ioctl(dsp_fd, SNDCTL_DSP_SPEED, &snd_rate) == -1)
293          {          {
294                  perror("SNDCTL_DSP_SPEED");                  int rates[] = { 44100, 48000, 0 };
295                  close(g_dsp_fd);                  int *prates = rates;
296                  return False;  
297                    while (*prates != 0)
298                    {
299                            if ((pwfx->nSamplesPerSec != *prates)
300                                && (ioctl(dsp_fd, SNDCTL_DSP_SPEED, prates) != -1))
301                            {
302                                    oss_driver.need_resampling = 1;
303                                    snd_rate = *prates;
304                                    if (rdpsnd_dsp_resample_set
305                                        (snd_rate, pwfx->wBitsPerSample, pwfx->nChannels) == False)
306                                    {
307                                            error("rdpsnd_dsp_resample_set failed");
308                                            oss_close();
309                                            return False;
310                                    }
311    
312                                    break;
313                            }
314                            prates++;
315                    }
316    
317                    if (*prates == 0)
318                    {
319                            perror("SNDCTL_DSP_SPEED");
320                            oss_close();
321                            return False;
322                    }
323          }          }
324    
325          /* try to get 12 fragments of 2^12 bytes size */          /* try to get 12 fragments of 2^12 bytes size */
326          fragments = (12 << 16) + 12;          fragments = (12 << 16) + 12;
327          ioctl(g_dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments);          ioctl(dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments);
328    
329          if (!driver_broken)          if (!driver_broken)
330          {          {
331                  audio_buf_info info;                  audio_buf_info info;
332    
333                  memset(&info, 0, sizeof(info));                  memset(&info, 0, sizeof(info));
334                  if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)                  if (ioctl(dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
335                  {                  {
336                          perror("SNDCTL_DSP_GETOSPACE");                          perror("SNDCTL_DSP_GETOSPACE");
337                          close(g_dsp_fd);                          oss_close();
338                          return False;                          return False;
339                  }                  }
340    
# Line 153  wave_out_set_format(WAVEFORMATEX * pwfx) Line 347  wave_out_set_format(WAVEFORMATEX * pwfx)
347                  }                  }
348          }          }
349    
350            dsp_configured = True;
351    
352          return True;          return True;
353  }  }
354    
355  void  void
356  wave_out_volume(uint16 left, uint16 right)  oss_volume(uint16 left, uint16 right)
357  {  {
         static BOOL use_dev_mixer = False;  
358          uint32 volume;          uint32 volume;
         int fd_mix = -1;  
359    
360          volume = left / (65536 / 100);          volume = left / (65536 / 100);
361          volume |= right / (65536 / 100) << 8;          volume |= right / (65536 / 100) << 8;
362    
363          if (use_dev_mixer)          if (ioctl(dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
         {  
                 if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1)  
                 {  
                         perror("open /dev/mixer");  
                         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)  
364          {          {
365                  perror("MIXER_WRITE(SOUND_MIXER_PCM)");                  warning("hardware volume control unavailable, falling back to software volume control!\n");
366                  use_dev_mixer = True;                  oss_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
367                    rdpsnd_dsp_softvol_set(left, right);
368                  return;                  return;
369          }          }
370  }  }
371    
372  void  void
373  wave_out_play(void)  oss_play(void)
374  {  {
375          struct audio_packet *packet;          struct audio_packet *packet;
376          ssize_t len;          ssize_t len;
377          STREAM out;          STREAM out;
         static long startedat_us;  
         static long startedat_s;  
         static BOOL started = False;  
         struct timeval tv;  
378    
379            /* We shouldn't be called if the queue is empty, but still */
380          if (rdpsnd_queue_empty())          if (rdpsnd_queue_empty())
         {  
                 g_dsp_busy = 0;  
381                  return;                  return;
         }  
382    
383          packet = rdpsnd_queue_current_packet();          packet = rdpsnd_queue_current_packet();
384          out = &packet->s;          out = &packet->s;
385    
         if (!started)  
         {  
                 gettimeofday(&tv, NULL);  
                 startedat_us = tv.tv_usec;  
                 startedat_s = tv.tv_sec;  
                 started = True;  
         }  
   
386          len = out->end - out->p;          len = out->end - out->p;
387    
388          len = write(g_dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len);          len = write(dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len);
389          if (len == -1)          if (len == -1)
390          {          {
391                  if (errno != EWOULDBLOCK)                  if (errno != EWOULDBLOCK)
392                          perror("write audio");                          perror("write audio");
                 g_dsp_busy = 1;  
393                  return;                  return;
394          }          }
395    
396          out->p += len;          out->p += len;
397    
398          if (out->p == out->end)          if (out->p == out->end)
399          {          {
400                  long long duration;                  int delay_bytes;
401                  long elapsed;                  unsigned long delay_us;
402                    audio_buf_info info;
                 gettimeofday(&tv, NULL);  
                 duration = (out->size * (1000000 / (samplewidth * snd_rate)));  
                 elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);  
403    
404                  if (elapsed >= (duration * 85) / 100)                  if (in_esddsp)
405                  {                  {
406                          /* We need to add 50 to tell windows that time has passed while                          /* EsounD has no way of querying buffer status, so we have to
407                           * playing this packet */                           * go with a fixed size. */
408                          rdpsnd_send_completion(packet->tick + 50, packet->index);                          delay_bytes = out->size;
                         rdpsnd_queue_next();  
                         started = False;  
409                  }                  }
410                  else                  else
411                  {                  {
412                          g_dsp_busy = 1;  #ifdef SNDCTL_DSP_GETODELAY
413                          return;                          delay_bytes = 0;
414                            if (ioctl(dsp_fd, SNDCTL_DSP_GETODELAY, &delay_bytes) == -1)
415                                    delay_bytes = -1;
416    #else
417                            delay_bytes = -1;
418    #endif
419    
420                            if (delay_bytes == -1)
421                            {
422                                    if (ioctl(dsp_fd, SNDCTL_DSP_GETOSPACE, &info) != -1)
423                                            delay_bytes = info.fragstotal * info.fragsize - info.bytes;
424                                    else
425                                            delay_bytes = out->size;
426                            }
427                    }
428    
429                    delay_us = delay_bytes * (1000000 / (samplewidth * snd_rate));
430                    rdpsnd_queue_next(delay_us);
431            }
432    }
433    
434    void
435    oss_record(void)
436    {
437            char buffer[32768];
438            int len;
439    
440            len = read(dsp_fd, buffer, sizeof(buffer));
441            if (len == -1)
442            {
443                    if (errno != EWOULDBLOCK)
444                            perror("read audio");
445                    return;
446            }
447    
448            rdpsnd_record(buffer, len);
449    }
450    
451    struct audio_driver *
452    oss_register(char *options)
453    {
454            memset(&oss_driver, 0, sizeof(oss_driver));
455    
456            oss_driver.name = "oss";
457            oss_driver.description =
458                    "OSS output driver, default device: " DEFAULTDEVICE " or $AUDIODEV";
459    
460            oss_driver.add_fds = oss_add_fds;
461            oss_driver.check_fds = oss_check_fds;
462    
463            oss_driver.wave_out_open = oss_open_out;
464            oss_driver.wave_out_close = oss_close_out;
465            oss_driver.wave_out_format_supported = oss_format_supported;
466            oss_driver.wave_out_set_format = oss_set_format;
467            oss_driver.wave_out_volume = oss_volume;
468    
469            oss_driver.wave_in_open = oss_open_in;
470            oss_driver.wave_in_close = oss_close_in;
471            oss_driver.wave_in_format_supported = oss_format_supported;
472            oss_driver.wave_in_set_format = oss_set_format;
473            oss_driver.wave_in_volume = NULL;       /* FIXME */
474    
475            oss_driver.need_byteswap_on_be = 0;
476            oss_driver.need_resampling = 0;
477    
478            if (options)
479            {
480                    dsp_dev = xstrdup(options);
481            }
482            else
483            {
484                    dsp_dev = getenv("AUDIODEV");
485    
486                    if (dsp_dev == NULL)
487                    {
488                            dsp_dev = DEFAULTDEVICE;
489                  }                  }
490          }          }
491          g_dsp_busy = 1;  
492          return;          return &oss_driver;
493  }  }

Legend:
Removed from v.1254  
changed lines
  Added in v.1365

  ViewVC Help
Powered by ViewVC 1.1.26