dsp: Implemented audio capture support.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 07 Aug 2016 02:43:20 -0400
changeset 102696b259657cfd1
parent 10268 a3faa23abdbb
child 10270 4d99b888e7e5
dsp: Implemented audio capture support.
src/audio/dsp/SDL_dspaudio.c
     1.1 --- a/src/audio/dsp/SDL_dspaudio.c	Sun Aug 07 01:48:38 2016 -0400
     1.2 +++ b/src/audio/dsp/SDL_dspaudio.c	Sun Aug 07 02:43:20 2016 -0400
     1.3 @@ -107,9 +107,8 @@
     1.4      if (this->hidden->audio_fd < 0) {
     1.5          return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
     1.6      }
     1.7 -    this->hidden->mixbuf = NULL;
     1.8  
     1.9 -    /* Make the file descriptor use blocking writes with fcntl() */
    1.10 +    /* Make the file descriptor use blocking i/o with fcntl() */
    1.11      {
    1.12          long ctlflags;
    1.13          ctlflags = fcntl(this->hidden->audio_fd, F_GETFL);
    1.14 @@ -236,12 +235,14 @@
    1.15  #endif
    1.16  
    1.17      /* Allocate mixing buffer */
    1.18 -    this->hidden->mixlen = this->spec.size;
    1.19 -    this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
    1.20 -    if (this->hidden->mixbuf == NULL) {
    1.21 -        return SDL_OutOfMemory();
    1.22 +    if (!iscapture) {
    1.23 +        this->hidden->mixlen = this->spec.size;
    1.24 +        this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
    1.25 +        if (this->hidden->mixbuf == NULL) {
    1.26 +            return SDL_OutOfMemory();
    1.27 +        }
    1.28 +        SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
    1.29      }
    1.30 -    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
    1.31  
    1.32      /* We're ready to rock and roll. :-) */
    1.33      return 0;
    1.34 @@ -251,14 +252,13 @@
    1.35  static void
    1.36  DSP_PlayDevice(_THIS)
    1.37  {
    1.38 -    const Uint8 *mixbuf = this->hidden->mixbuf;
    1.39 -    const int mixlen = this->hidden->mixlen;
    1.40 -    if (write(this->hidden->audio_fd, mixbuf, mixlen) == -1) {
    1.41 +    struct SDL_PrivateAudioData *h = this->hidden;
    1.42 +    if (write(h->audio_fd, h->mixbuf, h->mixlen) == -1) {
    1.43          perror("Audio write");
    1.44          SDL_OpenedAudioDeviceDisconnected(this);
    1.45      }
    1.46  #ifdef DEBUG_AUDIO
    1.47 -    fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
    1.48 +    fprintf(stderr, "Wrote %d bytes of audio data\n", h->mixlen);
    1.49  #endif
    1.50  }
    1.51  
    1.52 @@ -269,6 +269,30 @@
    1.53  }
    1.54  
    1.55  static int
    1.56 +DSP_CaptureFromDevice(_THIS, void *buffer, int buflen)
    1.57 +{
    1.58 +    return (int) read(this->hidden->audio_fd, buffer, buflen);
    1.59 +}
    1.60 +
    1.61 +static void
    1.62 +DSP_FlushCapture(_THIS)
    1.63 +{
    1.64 +    struct SDL_PrivateAudioData *h = this->hidden;
    1.65 +    audio_buf_info info;
    1.66 +    if (ioctl(h->audio_fd, SNDCTL_DSP_GETISPACE, &info) == 0) {
    1.67 +        while (info.bytes > 0) {
    1.68 +            char buf[512];
    1.69 +            const size_t len = SDL_min(sizeof (buf), info.bytes);
    1.70 +            const ssize_t br = read(h->audio_fd, buf, len);
    1.71 +            if (br <= 0) {
    1.72 +                break;
    1.73 +            }
    1.74 +            info.bytes -= br;
    1.75 +        }
    1.76 +    }
    1.77 +}
    1.78 +
    1.79 +static int
    1.80  DSP_Init(SDL_AudioDriverImpl * impl)
    1.81  {
    1.82      /* Set the function pointers */
    1.83 @@ -277,8 +301,11 @@
    1.84      impl->PlayDevice = DSP_PlayDevice;
    1.85      impl->GetDeviceBuf = DSP_GetDeviceBuf;
    1.86      impl->CloseDevice = DSP_CloseDevice;
    1.87 +    impl->CaptureFromDevice = DSP_CaptureFromDevice;
    1.88 +    impl->FlushCapture = DSP_FlushCapture;
    1.89  
    1.90      impl->AllowsArbitraryDeviceNames = 1;
    1.91 +    impl->HasCaptureSupport = SDL_TRUE;
    1.92  
    1.93      return 1;   /* this audio target is available. */
    1.94  }