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

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

revision 865 by stargo, Tue Mar 15 11:25:50 2005 UTC revision 1355 by stargo, Sun Dec 24 15:24:17 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 - SGI/IRIX     Sound Channel Process Functions - SGI/IRIX
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) Jeremy Meng voidfoo@cwazy.co.uk 2004     Copyright (C) Jeremy Meng void.foo@gmail.com 2004, 2005
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 24  Line 24 
24  #include <errno.h>  #include <errno.h>
25  #include <dmedia/audio.h>  #include <dmedia/audio.h>
26    
27  #define IRIX_DEBUG 1  /* #define IRIX_DEBUG 1 */
28    
29  #define IRIX_MAX_VOL     65535  #define IRIX_MAX_VOL     65535
30    
 #define MAX_QUEUE       10  
   
 int g_dsp_fd;  
31  ALconfig audioconfig;  ALconfig audioconfig;
32  ALport output_port;  ALport output_port;
33    
 BOOL g_dsp_busy = False;  
 static BOOL g_swapaudio;  
34  static int g_snd_rate;  static int g_snd_rate;
 static BOOL g_swapaudio;  
 static short g_samplewidth;  
35  static int width = AL_SAMPLE_16;  static int width = AL_SAMPLE_16;
36    static char *sgi_output_device = NULL;
37    
38  double min_volume, max_volume, volume_range;  double min_volume, max_volume, volume_range;
39  int resource, maxFillable;  int resource, maxFillable;
40  int combinedFrameSize;  int combinedFrameSize;
41    
42  static struct audio_packet  void sgi_play(void);
43    
44    void
45    sgi_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
46  {  {
47          struct stream s;          /* We need to be called rather often... */
48          uint16 tick;          FD_SET(0, wfds);
49          uint8 index;  }
50  } packet_queue[MAX_QUEUE];  
51  static unsigned int queue_hi, queue_lo;  void
52    sgi_check_fds(fd_set * rfds, fd_set * wfds)
53    {
54            if (output_port == (ALport) 0)
55                    return;
56    
57            if (!rdpsnd_queue_empty())
58                    sgi_play();
59    }
60    
61  BOOL  BOOL
62  wave_out_open(void)  sgi_open(void)
63  {  {
64          ALparamInfo pinfo;          ALparamInfo pinfo;
65            static int warned = 0;
66    
67  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
68          fprintf(stderr, "wave_out_open: begin\n");          fprintf(stderr, "sgi_open: begin\n");
69  #endif  #endif
70    
71            if (!warned && sgi_output_device)
72            {
73                    warning("device-options not supported for libao-driver\n");
74                    warned = 1;
75            }
76    
77          if (alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo) < 0)          if (alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo) < 0)
78          {          {
79                  fprintf(stderr, "wave_out_open: alGetParamInfo failed: %s\n",                  fprintf(stderr, "sgi_open: alGetParamInfo failed: %s\n",
80                          alGetErrorString(oserror()));                          alGetErrorString(oserror()));
81          }          }
82          min_volume = alFixedToDouble(pinfo.min.ll);          min_volume = alFixedToDouble(pinfo.min.ll);
83          max_volume = alFixedToDouble(pinfo.max.ll);          max_volume = alFixedToDouble(pinfo.max.ll);
84          volume_range = (max_volume - min_volume);          volume_range = (max_volume - min_volume);
85  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
86          fprintf(stderr, "wave_out_open: minvol = %lf, maxvol= %lf, range = %lf.\n",          fprintf(stderr, "sgi_open: minvol = %lf, maxvol= %lf, range = %lf.\n",
87                  min_volume, max_volume, volume_range);                  min_volume, max_volume, volume_range);
88  #endif  #endif
89    
         queue_lo = queue_hi = 0;  
   
90          audioconfig = alNewConfig();          audioconfig = alNewConfig();
91          if (audioconfig < 0)          if (audioconfig == (ALconfig) 0)
92          {          {
93                  fprintf(stderr, "wave_out_open: alNewConfig failed: %s\n",                  fprintf(stderr, "sgi_open: alNewConfig failed: %s\n", alGetErrorString(oserror()));
                         alGetErrorString(oserror()));  
94                  return False;                  return False;
95          }          }
96    
97          output_port = alOpenPort("rdpsnd", "w", 0);          output_port = alOpenPort("rdpsnd", "w", 0);
98          if (output_port == (ALport) 0)          if (output_port == (ALport) 0)
99          {          {
100                  fprintf(stderr, "wave_out_open: alOpenPort failed: %s\n",                  fprintf(stderr, "sgi_open: alOpenPort failed: %s\n", alGetErrorString(oserror()));
                         alGetErrorString(oserror()));  
101                  return False;                  return False;
102          }          }
103    
104  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
105          fprintf(stderr, "wave_out_open: returning\n");          fprintf(stderr, "sgi_open: returning\n");
106  #endif  #endif
107          return True;          return True;
108  }  }
109    
110  void  void
111  wave_out_close(void)  sgi_close(void)
112  {  {
113          /* Ack all remaining packets */          /* Ack all remaining packets */
114  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
115          fprintf(stderr, "wave_out_close: begin\n");          fprintf(stderr, "sgi_close: begin\n");
116  #endif  #endif
117    
118          while (queue_lo != queue_hi)          while (!rdpsnd_queue_empty())
119          {                  rdpsnd_queue_next(0);
                 rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index);  
                 free(packet_queue[queue_lo].s.data);  
                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
         }  
120          alDiscardFrames(output_port, 0);          alDiscardFrames(output_port, 0);
121    
122          alClosePort(output_port);          alClosePort(output_port);
123            output_port = (ALport) 0;
124          alFreeConfig(audioconfig);          alFreeConfig(audioconfig);
125  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
126          fprintf(stderr, "wave_out_close: returning\n");          fprintf(stderr, "sgi_close: returning\n");
127  #endif  #endif
128  }  }
129    
130  BOOL  BOOL
131  wave_out_format_supported(WAVEFORMATEX * pwfx)  sgi_format_supported(WAVEFORMATEX * pwfx)
132  {  {
133          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)          if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
134                  return False;                  return False;
# Line 136  wave_out_format_supported(WAVEFORMATEX * Line 141  wave_out_format_supported(WAVEFORMATEX *
141  }  }
142    
143  BOOL  BOOL
144  wave_out_set_format(WAVEFORMATEX * pwfx)  sgi_set_format(WAVEFORMATEX * pwfx)
145  {  {
146          int channels;          int channels;
147          int frameSize, channelCount;          int frameSize, channelCount;
148          ALpv params;          ALpv params;
149    
150  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
151          fprintf(stderr, "wave_out_set_format: init...\n");          fprintf(stderr, "sgi_set_format: init...\n");
152  #endif  #endif
         /* limited support to configure an opened audio port in IRIX */  
         /* have to reopen the audio port, using same config */  
         alClosePort(output_port);  
   
         g_swapaudio = False;  
153    
154          if (pwfx->wBitsPerSample == 8)          if (pwfx->wBitsPerSample == 8)
155                  width = AL_SAMPLE_8;                  width = AL_SAMPLE_8;
156          else if (pwfx->wBitsPerSample == 16)          else if (pwfx->wBitsPerSample == 16)
         {  
157                  width = AL_SAMPLE_16;                  width = AL_SAMPLE_16;
                 /* Do we need to swap the 16bit values? (Are we BigEndian) */  
 #if (defined(IRIX_DEBUG))  
                 g_swapaudio = 1;  
 #else  
                 g_swapaudio = 0;  
 #endif  
         }  
158    
159          g_samplewidth = pwfx->wBitsPerSample / 8;          /* Limited support to configure an opened audio port in IRIX.  The
160               number of channels is a static setting and can not be changed after
161               a port is opened.  So if the number of channels remains the same, we
162               can configure other settings; otherwise we have to reopen the audio
163               port, using same config. */
164    
165          channels = pwfx->nChannels;          channels = pwfx->nChannels;
166          g_snd_rate = pwfx->nSamplesPerSec;          g_snd_rate = pwfx->nSamplesPerSec;
167    
168          alSetSampFmt(audioconfig, AL_SAMPFMT_TWOSCOMP);          alSetSampFmt(audioconfig, AL_SAMPFMT_TWOSCOMP);
169          alSetWidth(audioconfig, width);          alSetWidth(audioconfig, width);
170          alSetChannels(audioconfig, channels);          if (channels != alGetChannels(audioconfig))
171            {
172                    alClosePort(output_port);
173                    alSetChannels(audioconfig, channels);
174                    output_port = alOpenPort("rdpsnd", "w", audioconfig);
175    
176          output_port = alOpenPort("rdpsnd", "w", audioconfig);                  if (output_port == (ALport) 0)
177                    {
178                            fprintf(stderr, "sgi_set_format: alOpenPort failed: %s\n",
179                                    alGetErrorString(oserror()));
180                            return False;
181                    }
182    
         if (output_port == (ALport) 0)  
         {  
                 fprintf(stderr, "wave_out_set_format: alOpenPort failed: %s\n",  
                         alGetErrorString(oserror()));  
                 return False;  
183          }          }
184    
185          resource = alGetResource(output_port);          resource = alGetResource(output_port);
# Line 188  wave_out_set_format(WAVEFORMATEX * pwfx) Line 189  wave_out_set_format(WAVEFORMATEX * pwfx)
189    
190          if (frameSize == 0 || channelCount == 0)          if (frameSize == 0 || channelCount == 0)
191          {          {
192                  fprintf(stderr, "wave_out_set_format: bad frameSize or channelCount\n");                  fprintf(stderr, "sgi_set_format: bad frameSize or channelCount\n");
193                  return False;                  return False;
194          }          }
195          combinedFrameSize = frameSize * channelCount;          combinedFrameSize = frameSize * channelCount;
# Line 209  wave_out_set_format(WAVEFORMATEX * pwfx) Line 210  wave_out_set_format(WAVEFORMATEX * pwfx)
210          }          }
211    
212  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
213          fprintf(stderr, "wave_out_set_format: returning...\n");          fprintf(stderr, "sgi_set_format: returning...\n");
214  #endif  #endif
215          return True;          return True;
216  }  }
217    
218  void  void
219  wave_out_volume(uint16 left, uint16 right)  sgi_volume(uint16 left, uint16 right)
220  {  {
221          double gainleft, gainright;          double gainleft, gainright;
222          ALpv pv[1];          ALpv pv[1];
223          ALfixed gain[8];          ALfixed gain[8];
224    
225  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
226          fprintf(stderr, "wave_out_volume: begin\n");          fprintf(stderr, "sgi_volume: begin\n");
227          fprintf(stderr, "left='%d', right='%d'\n", left, right);          fprintf(stderr, "left='%d', right='%d'\n", left, right);
228  #endif  #endif
229    
# Line 237  wave_out_volume(uint16 left, uint16 righ Line 238  wave_out_volume(uint16 left, uint16 righ
238          pv[0].sizeIn = 8;          pv[0].sizeIn = 8;
239          if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0)          if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0)
240          {          {
241                  fprintf(stderr, "wave_out_volume: alSetParams failed: %s\n",                  fprintf(stderr, "sgi_volume: alSetParams failed: %s\n",
242                          alGetErrorString(oserror()));                          alGetErrorString(oserror()));
243                  return;                  return;
244          }          }
245    
246  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
247          fprintf(stderr, "wave_out_volume: returning\n");          fprintf(stderr, "sgi_volume: returning\n");
248  #endif  #endif
249  }  }
250    
251  void  void
252  wave_out_write(STREAM s, uint16 tick, uint8 index)  sgi_play(void)
 {  
         struct audio_packet *packet = &packet_queue[queue_hi];  
         unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;  
   
         if (next_hi == queue_lo)  
         {  
                 fprintf(stderr, "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)  
253  {  {
254          struct audio_packet *packet;          struct audio_packet *packet;
255          ssize_t len;          ssize_t len;
256          unsigned int i;          unsigned int i;
         uint8 swap;  
257          STREAM out;          STREAM out;
         static long startedat_us;  
         static long startedat_s;  
         static BOOL started = False;  
         static BOOL swapped = False;  
         struct timeval tv;  
258          int gf;          int gf;
         static long long temp;  
259    
260          while (1)          while (1)
261          {          {
262                  if (queue_lo == queue_hi)                  if (rdpsnd_queue_empty())
                 {  
                         g_dsp_busy = False;  
263                          return;                          return;
                 }  
264    
265                  packet = &packet_queue[queue_lo];                  packet = rdpsnd_queue_current_packet();
266                  out = &packet->s;                  out = &packet->s;
267    
                 /* Swap the current packet, but only once */  
                 if (g_swapaudio && !swapped)  
                 {  
                         for (i = 0; i < out->end - out->p; i += 2)  
                         {  
                                 swap = *(out->p + i);  
                                 *(out->p + i) = *(out->p + i + 1);  
                                 *(out->p + i + 1) = swap;  
                         }  
                         swapped = True;  
                 }  
   
                 if (!started)  
                 {  
                         gettimeofday(&tv, NULL);  
                         startedat_us = tv.tv_usec;  
                         startedat_s = tv.tv_sec;  
                         started = True;  
                 }  
   
268                  len = out->end - out->p;                  len = out->end - out->p;
                 gf = alGetFillable(output_port);  
                 if (len > gf)  
                 {  
                         /* len = gf * combinedFrameSize; */  
 #if (defined(IRIX_DEBUG))  
                         /* fprintf(stderr,"Fillable...\n"); */  
 #endif  
                 }  
269    
270                  alWriteFrames(output_port, out->p, len / combinedFrameSize);                  alWriteFrames(output_port, out->p, len / combinedFrameSize);
271    
272                  out->p += len;                  out->p += len;
273                  if (out->p == out->end)                  if (out->p == out->end)
274                  {                  {
275                          long long duration;                          gf = alGetFilled(output_port);
276                          long elapsed;                          if (gf < (4 * maxFillable / 10))
   
                         gettimeofday(&tv, NULL);  
                         duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate)));  
                         elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);  
                         /* 7/10 is not good for IRIX audio port, 4x/100 is suitable */  
                         if (elapsed >= (duration * 485) / 1000)  
277                          {                          {
278                                  rdpsnd_send_completion(packet->tick, packet->index);                                  rdpsnd_queue_next(0);
                                 free(out->data);  
                                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
                                 started = False;  
                                 swapped = False;  
279                          }                          }
280                          else                          else
281                          {                          {
282  #if (defined(IRIX_DEBUG))  #if (defined(IRIX_DEBUG))
283                                  /* fprintf(stderr,"Busy playing...\n"); */  /*                              fprintf(stderr,"Busy playing...\n"); */
284  #endif  #endif
285                                  g_dsp_busy = True;                                  usleep(10);
286                                  return;                                  return;
287                          }                          }
288                  }                  }
289          }          }
290  }  }
291    
292    struct audio_driver *
293    sgi_register(char *options)
294    {
295            static struct audio_driver sgi_driver;
296    
297            sgi_driver.name = xstrdup("sgi");
298            sgi_driver.description = xstrdup("SGI output driver");
299    
300            sgi_driver.add_fds = sgi_add_fds;
301            sgi_driver.check_fds = sgi_check_fds;
302    
303            sgi_driver.wave_out_open = sgi_open;
304            sgi_driver.wave_out_close = sgi_close;
305            sgi_driver.wave_out_format_supported = sgi_format_supported;
306            sgi_driver.wave_out_set_format = sgi_set_format;
307            sgi_driver.wave_out_volume = sgi_volume;
308    
309            sgi_driver.need_byteswap_on_be = 1;
310            sgi_driver.need_resampling = 0;
311    
312            if (options)
313            {
314                    sgi_output_device = xstrdup(options);
315            }
316            return &sgi_driver;
317    }

Legend:
Removed from v.865  
changed lines
  Added in v.1355

  ViewVC Help
Powered by ViewVC 1.1.26