/[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 499 by astrand, Wed Oct 15 14:01:32 2003 UTC revision 532 by astrand, Wed Oct 29 14:14:46 2003 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    
30  #define MAX_QUEUE       10  #define MAX_QUEUE       10
31    
32  int g_dsp_fd;  int g_dsp_fd;
33  BOOL g_dsp_busy;  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 77  wave_out_format_supported(WAVEFORMATEX * Line 80  wave_out_format_supported(WAVEFORMATEX *
80  BOOL  BOOL
81  wave_out_set_format(WAVEFORMATEX * pwfx)  wave_out_set_format(WAVEFORMATEX * pwfx)
82  {  {
83          int speed, channels, format;          int channels, format;
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 87  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;
94    
95          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)          if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
96          {          {
97                  perror("SNDCTL_DSP_SETFMT");                  perror("SNDCTL_DSP_SETFMT");
# Line 102  wave_out_set_format(WAVEFORMATEX * pwfx) Line 107  wave_out_set_format(WAVEFORMATEX * pwfx)
107                  return False;                  return False;
108          }          }
109    
110          speed = pwfx->nSamplesPerSec;          if (channels == 2)
111          if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &speed) == -1)          {
112                    g_samplewidth *= 2;
113            }
114    
115            g_snd_rate = pwfx->nSamplesPerSec;
116            if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1)
117          {          {
118                  perror("SNDCTL_DSP_SPEED");                  perror("SNDCTL_DSP_SPEED");
119                  close(g_dsp_fd);                  close(g_dsp_fd);
# Line 116  wave_out_set_format(WAVEFORMATEX * pwfx) Line 126  wave_out_set_format(WAVEFORMATEX * pwfx)
126  void  void
127  wave_out_volume(uint16 left, uint16 right)  wave_out_volume(uint16 left, uint16 right)
128  {  {
129            static BOOL use_dev_mixer = False;
130          uint32 volume;          uint32 volume;
131            int fd_mix = -1;
132    
133          volume = left / (65536 / 100);          volume = left / (65536 / 100);
134          volume |= right / (65536 / 100) << 8;          volume |= right / (65536 / 100) << 8;
135    
136            if (use_dev_mixer)
137            {
138                    if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1)
139                    {
140                            perror("open /dev/mixer");
141                            return;
142                    }
143    
144                    if (ioctl(fd_mix, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
145                    {
146                            perror("MIXER_WRITE(SOUND_MIXER_PCM)");
147                            return;
148                    }
149    
150                    close(fd_mix);
151            }
152    
153          if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)          if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
154          {          {
155                  perror("MIXER_WRITE(SOUND_MIXER_PCM)");                  perror("MIXER_WRITE(SOUND_MIXER_PCM)");
156                    use_dev_mixer = True;
157                  return;                  return;
158          }          }
159  }  }
# Line 159  wave_out_play(void) Line 190  wave_out_play(void)
190          struct audio_packet *packet;          struct audio_packet *packet;
191          ssize_t len;          ssize_t len;
192          STREAM out;          STREAM out;
193            static long startedat_us;
194            static long startedat_s;
195            static BOOL started = False;
196            struct timeval tv;
197    
198          while (1)          while (1)
199          {          {
# Line 171  wave_out_play(void) Line 206  wave_out_play(void)
206                  packet = &packet_queue[queue_lo];                  packet = &packet_queue[queue_lo];
207                  out = &packet->s;                  out = &packet->s;
208    
209                    if (!started)
210                    {
211                            gettimeofday(&tv, NULL);
212                            startedat_us = tv.tv_usec;
213                            startedat_s = tv.tv_sec;
214                            started = True;
215                    }
216    
217                  len = write(g_dsp_fd, out->p, out->end - out->p);                  len = write(g_dsp_fd, out->p, out->end - out->p);
218                  if (len == -1)                  if (len == -1)
219                  {                  {
# Line 183  wave_out_play(void) Line 226  wave_out_play(void)
226                  out->p += len;                  out->p += len;
227                  if (out->p == out->end)                  if (out->p == out->end)
228                  {                  {
229                          rdpsnd_send_completion(packet->tick, packet->index);                          long long duration;
230                          free(out->data);                          long elapsed;
231                          queue_lo = (queue_lo + 1) % MAX_QUEUE;  
232                            gettimeofday(&tv, NULL);
233                            duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate)));
234                            elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us);
235    
236                            if (elapsed >= (duration * 7) / 10)
237                            {
238                                    rdpsnd_send_completion(packet->tick, packet->index);
239                                    free(out->data);
240                                    queue_lo = (queue_lo + 1) % MAX_QUEUE;
241                                    started = False;
242                            }
243                            else
244                            {
245                                    g_dsp_busy = 1;
246                                    return;
247                            }
248                  }                  }
249          }          }
   
250  }  }

Legend:
Removed from v.499  
changed lines
  Added in v.532

  ViewVC Help
Powered by ViewVC 1.1.26