src/audio/SDL_audio.c
changeset 9012 aa058c87737b
parent 9010 6d059ed9b6ca
child 9031 e963a13a720c
     1.1 --- a/src/audio/SDL_audio.c	Sat Jul 26 16:52:26 2014 -0700
     1.2 +++ b/src/audio/SDL_audio.c	Tue Jul 22 21:41:49 2014 -0400
     1.3 @@ -324,6 +324,181 @@
     1.4  }
     1.5  #endif
     1.6  
     1.7 +
     1.8 +/* buffer queueing support... */
     1.9 +
    1.10 +/* this expects that you managed thread safety elsewhere. */
    1.11 +static void
    1.12 +free_audio_queue(SDL_AudioBufferQueue *buffer)
    1.13 +{
    1.14 +    while (buffer) {
    1.15 +        SDL_AudioBufferQueue *next = buffer->next;
    1.16 +        SDL_free(buffer);
    1.17 +        buffer = next;
    1.18 +    }
    1.19 +}
    1.20 +
    1.21 +static void SDLCALL
    1.22 +SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int _len)
    1.23 +{
    1.24 +    /* this function always holds the mixer lock before being called. */
    1.25 +    Uint32 len = (Uint32) _len;
    1.26 +    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
    1.27 +    SDL_AudioBufferQueue *buffer;
    1.28 +
    1.29 +    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
    1.30 +    SDL_assert(_len >= 0);  /* this shouldn't ever happen, right?! */
    1.31 +
    1.32 +    while ((len > 0) && ((buffer = device->buffer_queue_head) != NULL)) {
    1.33 +        const Uint32 avail = buffer->datalen - buffer->startpos;
    1.34 +        const Uint32 cpy = SDL_min(len, avail);
    1.35 +        SDL_assert(device->queued_bytes >= avail);
    1.36 +
    1.37 +        SDL_memcpy(stream, buffer->data + buffer->startpos, cpy);
    1.38 +        buffer->startpos += cpy;
    1.39 +        stream += cpy;
    1.40 +        device->queued_bytes -= cpy;
    1.41 +        len -= cpy;
    1.42 +
    1.43 +        if (buffer->startpos == buffer->datalen) {  /* packet is done, put it in the pool. */
    1.44 +            device->buffer_queue_head = buffer->next;
    1.45 +            SDL_assert((buffer->next != NULL) || (buffer == device->buffer_queue_tail));
    1.46 +            buffer->next = device->buffer_queue_pool;
    1.47 +            device->buffer_queue_pool = buffer;
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
    1.52 +
    1.53 +    if (len > 0) {  /* fill any remaining space in the stream with silence. */
    1.54 +        SDL_assert(device->buffer_queue_head == NULL);
    1.55 +        SDL_memset(stream, device->spec.silence, len);
    1.56 +    }
    1.57 +
    1.58 +    if (device->buffer_queue_head == NULL) {
    1.59 +        device->buffer_queue_tail = NULL;  /* in case we drained the queue entirely. */
    1.60 +    }
    1.61 +}
    1.62 +
    1.63 +int
    1.64 +SDL_QueueAudio(SDL_AudioDeviceID devid, const void *_data, Uint32 len)
    1.65 +{
    1.66 +    SDL_AudioDevice *device = get_audio_device(devid);
    1.67 +    const Uint8 *data = (const Uint8 *) _data;
    1.68 +    SDL_AudioBufferQueue *orighead;
    1.69 +    SDL_AudioBufferQueue *origtail;
    1.70 +    Uint32 origlen;
    1.71 +    Uint32 datalen;
    1.72 +
    1.73 +    if (!device) {
    1.74 +        return -1;  /* get_audio_device() will have set the error state */
    1.75 +    }
    1.76 +
    1.77 +    if (device->spec.callback != SDL_BufferQueueDrainCallback) {
    1.78 +        return SDL_SetError("Audio device has a callback, queueing not allowed");
    1.79 +    }
    1.80 +
    1.81 +    current_audio.impl.LockDevice(device);
    1.82 +
    1.83 +    orighead = device->buffer_queue_head;
    1.84 +    origtail = device->buffer_queue_tail;
    1.85 +    origlen = origtail ? origtail->datalen : 0;
    1.86 +
    1.87 +    while (len > 0) {
    1.88 +        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
    1.89 +        SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
    1.90 +        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
    1.91 +            /* tail packet missing or completely full; we need a new packet. */
    1.92 +            packet = device->buffer_queue_pool;
    1.93 +            if (packet != NULL) {
    1.94 +                /* we have one available in the pool. */
    1.95 +                device->buffer_queue_pool = packet->next;
    1.96 +            } else {
    1.97 +                /* Have to allocate a new one! */
    1.98 +                packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
    1.99 +                if (packet == NULL) {
   1.100 +                    /* uhoh, reset so we've queued nothing new, free what we can. */
   1.101 +                    if (!origtail) {
   1.102 +                        packet = device->buffer_queue_head;  /* whole queue. */
   1.103 +                    } else {
   1.104 +                        packet = origtail->next;  /* what we added to existing queue. */
   1.105 +                        origtail->next = NULL;
   1.106 +                        origtail->datalen = origlen;
   1.107 +                    }
   1.108 +                    device->buffer_queue_head = orighead;
   1.109 +                    device->buffer_queue_tail = origtail;
   1.110 +                    device->buffer_queue_pool = NULL;
   1.111 +
   1.112 +                    current_audio.impl.UnlockDevice(device);
   1.113 +
   1.114 +                    free_audio_queue(packet);  /* give back what we can. */
   1.115 +
   1.116 +                    return SDL_OutOfMemory();
   1.117 +                }
   1.118 +            }
   1.119 +            packet->datalen = 0;
   1.120 +            packet->startpos = 0;
   1.121 +            packet->next = NULL;
   1.122 +
   1.123 +            SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
   1.124 +            if (device->buffer_queue_tail == NULL) {
   1.125 +                device->buffer_queue_head = packet;
   1.126 +            } else {
   1.127 +                device->buffer_queue_tail->next = packet;
   1.128 +            }
   1.129 +            device->buffer_queue_tail = packet;
   1.130 +        }
   1.131 +
   1.132 +        datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
   1.133 +        SDL_memcpy(packet->data + packet->datalen, data, datalen);
   1.134 +        data += datalen;
   1.135 +        len -= datalen;
   1.136 +        packet->datalen += datalen;
   1.137 +        device->queued_bytes += datalen;
   1.138 +    }
   1.139 +
   1.140 +    current_audio.impl.UnlockDevice(device);
   1.141 +
   1.142 +    return 0;
   1.143 +}
   1.144 +
   1.145 +Uint32
   1.146 +SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
   1.147 +{
   1.148 +    /* this happens to work for non-queueing devices, since we memset()
   1.149 +       the device to zero at init time, and these devices should return 0. */
   1.150 +    Uint32 retval = 0;
   1.151 +    SDL_AudioDevice *device = get_audio_device(devid);
   1.152 +    if (device) {
   1.153 +        current_audio.impl.LockDevice(device);
   1.154 +        retval = device->queued_bytes;
   1.155 +        current_audio.impl.UnlockDevice(device);
   1.156 +    }
   1.157 +
   1.158 +    return retval;
   1.159 +}
   1.160 +
   1.161 +void
   1.162 +SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
   1.163 +{
   1.164 +    SDL_AudioDevice *device = get_audio_device(devid);
   1.165 +    SDL_AudioBufferQueue *buffer = NULL;
   1.166 +    if (!device) {
   1.167 +        return;  /* nothing to do. */
   1.168 +    }
   1.169 +
   1.170 +    /* Blank out the device and release the mutex. Free it afterwards. */
   1.171 +    current_audio.impl.LockDevice(device);
   1.172 +    buffer = device->buffer_queue_head;
   1.173 +    device->buffer_queue_tail = NULL;
   1.174 +    device->buffer_queue_head = NULL;
   1.175 +    device->queued_bytes = 0;
   1.176 +    current_audio.impl.UnlockDevice(device);
   1.177 +
   1.178 +    free_audio_queue(buffer);
   1.179 +}
   1.180 +
   1.181 +
   1.182  #if defined(__ANDROID__)
   1.183  #include <android/log.h>
   1.184  #endif
   1.185 @@ -800,6 +975,10 @@
   1.186          current_audio.impl.CloseDevice(device);
   1.187          device->opened = 0;
   1.188      }
   1.189 +
   1.190 +    free_audio_queue(device->buffer_queue_head);
   1.191 +    free_audio_queue(device->buffer_queue_pool);
   1.192 +
   1.193      SDL_FreeAudioMem(device);
   1.194  }
   1.195  
   1.196 @@ -814,11 +993,6 @@
   1.197  {
   1.198      SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
   1.199  
   1.200 -    if (orig->callback == NULL) {
   1.201 -        SDL_SetError("SDL_OpenAudio() passed a NULL callback");
   1.202 -        return 0;
   1.203 -    }
   1.204 -
   1.205      if (orig->freq == 0) {
   1.206          const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
   1.207          if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
   1.208 @@ -871,7 +1045,6 @@
   1.209      return 1;
   1.210  }
   1.211  
   1.212 -
   1.213  static SDL_AudioDeviceID
   1.214  open_audio_device(const char *devname, int iscapture,
   1.215                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
   1.216 @@ -950,7 +1123,7 @@
   1.217          SDL_OutOfMemory();
   1.218          return 0;
   1.219      }
   1.220 -    SDL_memset(device, '\0', sizeof(SDL_AudioDevice));
   1.221 +    SDL_zerop(device);
   1.222      device->spec = *obtained;
   1.223      device->enabled = 1;
   1.224      device->paused = 1;
   1.225 @@ -968,8 +1141,9 @@
   1.226  
   1.227      /* force a device detection if we haven't done one yet. */
   1.228      if ( ((iscapture) && (current_audio.inputDevices == NULL)) ||
   1.229 -         ((!iscapture) && (current_audio.outputDevices == NULL)) )
   1.230 +         ((!iscapture) && (current_audio.outputDevices == NULL)) ) {
   1.231          SDL_GetNumAudioDevices(iscapture);
   1.232 +    }
   1.233  
   1.234      if (current_audio.impl.OpenDevice(device, devname, iscapture) < 0) {
   1.235          close_audio_device(device);
   1.236 @@ -1043,6 +1217,25 @@
   1.237          }
   1.238      }
   1.239  
   1.240 +    if (device->spec.callback == NULL) {  /* use buffer queueing? */
   1.241 +        /* pool a few packets to start. Enough for two callbacks. */
   1.242 +        const int packetlen = SDL_AUDIOBUFFERQUEUE_PACKETLEN;
   1.243 +        const int wantbytes = ((device->convert.needed) ? device->convert.len : device->spec.size) * 2;
   1.244 +        const int wantpackets = (wantbytes / packetlen) + ((wantbytes % packetlen) ? packetlen : 0);
   1.245 +        for (i = 0; i < wantpackets; i++) {
   1.246 +            SDL_AudioBufferQueue *packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
   1.247 +            if (packet) { /* don't care if this fails, we'll deal later. */
   1.248 +                packet->datalen = 0;
   1.249 +                packet->startpos = 0;
   1.250 +                packet->next = device->buffer_queue_pool;
   1.251 +                device->buffer_queue_pool = packet;
   1.252 +            }
   1.253 +        }
   1.254 +
   1.255 +        device->spec.callback = SDL_BufferQueueDrainCallback;
   1.256 +        device->spec.userdata = device;
   1.257 +    }
   1.258 +
   1.259      /* Find an available device ID and store the structure... */
   1.260      for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
   1.261          if (open_devices[id] == NULL) {