/[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 504 by stargo, Sun Oct 19 11:59:41 2003 UTC revision 753 by stargo, Mon Aug 23 11:13:50 2004 UTC
# Line 23  Line 23 
23  #include <unistd.h>  #include <unistd.h>
24  #include <fcntl.h>  #include <fcntl.h>
25  #include <errno.h>  #include <errno.h>
26    #include <sys/time.h>
27  #include <sys/ioctl.h>  #include <sys/ioctl.h>
28  #include <sys/soundcard.h>  #include <sys/soundcard.h>
29    
# Line 30  Line 31 
31    
32  int g_dsp_fd;  int g_dsp_fd;
33  BOOL g_dsp_busy = False;  BOOL g_dsp_busy = False;
34    static int g_snd_rate;
35    static short g_samplewidth;
36    
37  static struct audio_packet  static struct audio_packet
38  {  {
# Line 42  static unsigned int queue_hi, queue_lo; Line 45  static unsigned int queue_hi, queue_lo;
45  BOOL  BOOL
46  wave_out_open(void)  wave_out_open(void)
47  {  {
48          char *dsp_dev = "/dev/dsp";          char *dsp_dev = getenv("AUDIODEV");
49    
50            if (dsp_dev == NULL)
51            {
52                    dsp_dev = "/dev/dsp";
53            }
54    
55          if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)          if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)
56          {          {
# Line 77  wave_out_format_supported(WAVEFORMATEX * Line 85  wave_out_format_supported(WAVEFORMATEX *
85  BOOL  BOOL
86  wave_out_set_format(WAVEFORMATEX * pwfx)  wave_out_set_format(WAVEFORMATEX * pwfx)
87  {  {
88          int speed, channels, format;          int channels, format;
89    
90          ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);          ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);
91          ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);          ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);
# Line 87  wave_out_set_format(WAVEFORMATEX * pwfx) Line 95  wave_out_set_format(WAVEFORMATEX * pwfx)
95          else if (pwfx->wBitsPerSample == 16)          else if (pwfx->wBitsPerSample == 16)
96                  format = AFMT_S16_LE;                  format = AFMT_S16_LE;
97    
98            g_samplewidth = pwfx->wBitsPerSample / 8;
99    
100          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
101          {          {
102                  perror("SNDCTL_DSP_SETFMT");                  perror("SNDCTL_DSP_SETFMT");
# Line 102  wave_out_set_format(WAVEFORMATEX * pwfx) Line 112  wave_out_set_format(WAVEFORMATEX * pwfx)
112                  return False;                  return False;
113          }          }
114    
115          speed = pwfx->nSamplesPerSec;          if (channels == 2)
116          if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &speed) == -1)          {
117                    g_samplewidth *= 2;
118            }
119    
120            g_snd_rate = pwfx->nSamplesPerSec;
121            if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1)
122          {          {
123                  perror("SNDCTL_DSP_SPEED");                  perror("SNDCTL_DSP_SPEED");
124                  close(g_dsp_fd);                  close(g_dsp_fd);
# Line 116  wave_out_set_format(WAVEFORMATEX * pwfx) Line 131  wave_out_set_format(WAVEFORMATEX * pwfx)
131  void  void
132  wave_out_volume(uint16 left, uint16 right)  wave_out_volume(uint16 left, uint16 right)
133  {  {
134            static BOOL use_dev_mixer = False;
135          uint32 volume;          uint32 volume;
136            int fd_mix = -1;
137    
138          volume = left / (65536 / 100);          volume = left / (65536 / 100);
139          volume |= right / (65536 / 100) << 8;          volume |= right / (65536 / 100) << 8;
140    
141            if (use_dev_mixer)
142            {
143                    if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1)
144                    {
145                            perror("open /dev/mixer");
146                            return;
147                    }
148    
149                    if (ioctl(fd_mix, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
150                    {
151                            perror("MIXER_WRITE(SOUND_MIXER_PCM)");
152                            return;
153                    }
154    
155                    close(fd_mix);
156            }
157    
158          if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)          if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
159          {          {
160                  perror("MIXER_WRITE(SOUND_MIXER_PCM)");                  perror("MIXER_WRITE(SOUND_MIXER_PCM)");
161                    use_dev_mixer = True;
162                  return;                  return;
163          }          }
164  }  }
# Line 159  wave_out_play(void) Line 195  wave_out_play(void)
195          struct audio_packet *packet;          struct audio_packet *packet;
196          ssize_t len;          ssize_t len;
197          STREAM out;          STREAM out;
198            static long startedat_us;
199            static long startedat_s;
200            static BOOL started = False;
201            struct timeval tv;
202    
203          while (1)          while (1)
204          {          {
# Line 171  wave_out_play(void) Line 211  wave_out_play(void)
211                  packet = &packet_queue[queue_lo];                  packet = &packet_queue[queue_lo];
212                  out = &packet->s;                  out = &packet->s;
213    
214                    if (!started)
215                    {
216                            gettimeofday(&tv, NULL);
217                            startedat_us = tv.tv_usec;
218                            startedat_s = tv.tv_sec;
219                            started = True;
220                    }
221    
222                  len = write(g_dsp_fd, out->p, out->end - out->p);                  len = write(g_dsp_fd, out->p, out->end - out->p);
223                  if (len == -1)                  if (len == -1)
224                  {                  {
# Line 183  wave_out_play(void) Line 231  wave_out_play(void)
231                  out->p += len;                  out->p += len;
232                  if (out->p == out->end)                  if (out->p == out->end)
233                  {                  {
234                          rdpsnd_send_completion(packet->tick, packet->index);                          long long duration;
235                          free(out->data);                          long elapsed;
236                          queue_lo = (queue_lo + 1) % MAX_QUEUE;  
237                            gettimeofday(&tv, NULL);
238                            duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate)));
239                            elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);
240    
241                            if (elapsed >= (duration * 85) / 100)
242                            {
243                                    rdpsnd_send_completion(packet->tick, packet->index);
244                                    free(out->data);
245                                    queue_lo = (queue_lo + 1) % MAX_QUEUE;
246                                    started = False;
247                            }
248                            else
249                            {
250                                    g_dsp_busy = 1;
251                                    return;
252                            }
253                  }                  }
254          }          }
   
255  }  }

Legend:
Removed from v.504  
changed lines
  Added in v.753

  ViewVC Help
Powered by ViewVC 1.1.26