Convert remaining ringbuffers to the lockless variant

This commit is contained in:
Chris Robinson
2016-03-29 23:48:36 -07:00
parent 2ccc1d1d8a
commit d6163fe570
5 changed files with 74 additions and 70 deletions
+16 -19
View File
@@ -915,7 +915,7 @@ typedef struct ALCcaptureAlsa {
ALsizei size;
ALboolean doCapture;
RingBuffer *ring;
ll_ringbuffer_t *ring;
snd_pcm_sframes_t last_avail;
} ALCcaptureAlsa;
@@ -1047,21 +1047,15 @@ static ALCenum ALCcaptureAlsa_open(ALCcaptureAlsa *self, const ALCchar *name)
if(needring)
{
self->ring = CreateRingBuffer(FrameSizeFromDevFmt(device->FmtChans, device->FmtType),
device->UpdateSize*device->NumUpdates);
self->ring = ll_ringbuffer_create(
device->UpdateSize*device->NumUpdates + 1,
FrameSizeFromDevFmt(device->FmtChans, device->FmtType)
);
if(!self->ring)
{
ERR("ring buffer create failed\n");
goto error2;
}
self->size = snd_pcm_frames_to_bytes(self->pcmHandle, periodSizeInFrames);
self->buffer = malloc(self->size);
if(!self->buffer)
{
ERR("buffer malloc failed\n");
goto error2;
}
}
al_string_copy_cstr(&device->DeviceName, name);
@@ -1075,7 +1069,7 @@ error:
error2:
free(self->buffer);
self->buffer = NULL;
DestroyRingBuffer(self->ring);
ll_ringbuffer_free(self->ring);
self->ring = NULL;
snd_pcm_close(self->pcmHandle);
@@ -1085,7 +1079,7 @@ error2:
static void ALCcaptureAlsa_close(ALCcaptureAlsa *self)
{
snd_pcm_close(self->pcmHandle);
DestroyRingBuffer(self->ring);
ll_ringbuffer_free(self->ring);
free(self->buffer);
self->buffer = NULL;
@@ -1143,7 +1137,7 @@ static ALCenum ALCcaptureAlsa_captureSamples(ALCcaptureAlsa *self, ALCvoid *buff
if(self->ring)
{
ReadRingBuffer(self->ring, buffer, samples);
ll_ringbuffer_read(self->ring, buffer, samples);
return ALC_NO_ERROR;
}
@@ -1246,12 +1240,15 @@ static ALCuint ALCcaptureAlsa_availableSamples(ALCcaptureAlsa *self)
while(avail > 0)
{
ll_ringbuffer_data_t vec[2];
snd_pcm_sframes_t amt;
amt = snd_pcm_bytes_to_frames(self->pcmHandle, self->size);
if(avail < amt) amt = avail;
ll_ringbuffer_get_write_vector(self->ring, vec);
if(vec[0].len == 0) break;
amt = snd_pcm_readi(self->pcmHandle, self->buffer, amt);
amt = (vec[0].len < (snd_pcm_uframes_t)avail) ?
vec[0].len : (snd_pcm_uframes_t)avail;
amt = snd_pcm_readi(self->pcmHandle, vec[0].buf, amt);
if(amt < 0)
{
ERR("read error: %s\n", snd_strerror(amt));
@@ -1275,11 +1272,11 @@ static ALCuint ALCcaptureAlsa_availableSamples(ALCcaptureAlsa *self)
continue;
}
WriteRingBuffer(self->ring, self->buffer, amt);
ll_ringbuffer_write_advance(self->ring, amt);
avail -= amt;
}
return RingBufferSize(self->ring);
return ll_ringbuffer_read_space(self->ring);
}
static ALint64 ALCcaptureAlsa_getLatency(ALCcaptureAlsa *self)
+13 -9
View File
@@ -45,7 +45,7 @@ typedef struct {
AudioBufferList *bufferList; // Buffer for data coming from the input device
ALCvoid *resampleBuffer; // Buffer for returned RingBuffer data when resampling
RingBuffer *ring;
ll_ringbuffer_t *ring;
} ca_data;
static const ALCchar ca_device[] = "CoreAudio Default";
@@ -102,7 +102,7 @@ static OSStatus ca_capture_conversion_callback(AudioConverterRef inAudioConverte
ca_data *data = (ca_data*)device->ExtraData;
// Read from the ring buffer and store temporarily in a large buffer
ReadRingBuffer(data->ring, data->resampleBuffer, (ALsizei)(*ioNumberDataPackets));
ll_ringbuffer_read(data->ring, data->resampleBuffer, *ioNumberDataPackets);
// Set the input data
ioData->mNumberBuffers = 1;
@@ -130,7 +130,7 @@ static OSStatus ca_capture_callback(void *inRefCon, AudioUnitRenderActionFlags *
return err;
}
WriteRingBuffer(data->ring, data->bufferList->mBuffers[0].mData, inNumberFrames);
ll_ringbuffer_write(data->ring, data->bufferList->mBuffers[0].mData, inNumberFrames);
return noErr;
}
@@ -586,16 +586,19 @@ static ALCenum ca_open_capture(ALCdevice *device, const ALCchar *deviceName)
if(data->bufferList == NULL)
goto error;
data->ring = CreateRingBuffer(data->frameSize, (device->UpdateSize * data->sampleRateRatio) * device->NumUpdates);
if(data->ring == NULL)
goto error;
data->ring = ll_ringbuffer_create(
device->UpdateSize*data->sampleRateRatio*device->NumUpdates + 1,
data->frameSize
);
if(!data->ring) goto error;
al_string_copy_cstr(&device->DeviceName, deviceName);
return ALC_NO_ERROR;
error:
DestroyRingBuffer(data->ring);
ll_ringbuffer_free(data->ring);
data->ring = NULL;
free(data->resampleBuffer);
destroy_buffer_list(data->bufferList);
@@ -614,7 +617,8 @@ static void ca_close_capture(ALCdevice *device)
{
ca_data *data = (ca_data*)device->ExtraData;
DestroyRingBuffer(data->ring);
ll_ringbuffer_free(data->ring);
data->ring = NULL;
free(data->resampleBuffer);
destroy_buffer_list(data->bufferList);
@@ -676,7 +680,7 @@ static ALCenum ca_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint sa
static ALCuint ca_available_samples(ALCdevice *device)
{
ca_data *data = device->ExtraData;
return RingBufferSize(data->ring) / data->sampleRateRatio;
return ll_ringbuffer_read_space(data->ring) / data->sampleRateRatio;
}
+10 -8
View File
@@ -653,7 +653,8 @@ typedef struct ALCdsoundCapture {
IDirectSoundCaptureBuffer *DSCbuffer;
DWORD BufferBytes;
DWORD Cursor;
RingBuffer *Ring;
ll_ringbuffer_t *Ring;
} ALCdsoundCapture;
static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device);
@@ -823,7 +824,8 @@ static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *devi
}
if(SUCCEEDED(hr))
{
self->Ring = CreateRingBuffer(InputType.Format.nBlockAlign, device->UpdateSize * device->NumUpdates);
self->Ring = ll_ringbuffer_create(device->UpdateSize*device->NumUpdates + 1,
InputType.Format.nBlockAlign);
if(self->Ring == NULL)
hr = DSERR_OUTOFMEMORY;
}
@@ -832,7 +834,7 @@ static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *devi
{
ERR("Device init failed: 0x%08lx\n", hr);
DestroyRingBuffer(self->Ring);
ll_ringbuffer_free(self->Ring);
self->Ring = NULL;
if(self->DSCbuffer != NULL)
IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
@@ -854,7 +856,7 @@ static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *devi
static void ALCdsoundCapture_close(ALCdsoundCapture *self)
{
DestroyRingBuffer(self->Ring);
ll_ringbuffer_free(self->Ring);
self->Ring = NULL;
if(self->DSCbuffer != NULL)
@@ -897,7 +899,7 @@ static void ALCdsoundCapture_stop(ALCdsoundCapture *self)
static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples)
{
ReadRingBuffer(self->Ring, buffer, samples);
ll_ringbuffer_read(self->Ring, buffer, samples);
return ALC_NO_ERROR;
}
@@ -929,9 +931,9 @@ static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self)
}
if(SUCCEEDED(hr))
{
WriteRingBuffer(self->Ring, ReadPtr1, ReadCnt1/FrameSize);
ll_ringbuffer_write(self->Ring, ReadPtr1, ReadCnt1/FrameSize);
if(ReadPtr2 != NULL)
WriteRingBuffer(self->Ring, ReadPtr2, ReadCnt2/FrameSize);
ll_ringbuffer_write(self->Ring, ReadPtr2, ReadCnt2/FrameSize);
hr = IDirectSoundCaptureBuffer_Unlock(self->DSCbuffer,
ReadPtr1, ReadCnt1,
ReadPtr2, ReadCnt2);
@@ -945,7 +947,7 @@ static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self)
}
done:
return RingBufferSize(self->Ring);
return ll_ringbuffer_read_space(self->Ring);
}
+26 -25
View File
@@ -484,10 +484,7 @@ typedef struct ALCcaptureOSS {
int fd;
ALubyte *read_data;
int data_size;
RingBuffer *ring;
ll_ringbuffer_t *ring;
int doCapture;
volatile int killNow;
@@ -517,7 +514,7 @@ static int ALCcaptureOSS_recordProc(void *ptr)
ALCcaptureOSS *self = (ALCcaptureOSS*)ptr;
ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
int frameSize;
int amt;
ssize_t amt;
SetRTPriority();
althrd_setname(althrd_current(), RECORD_THREAD_NAME);
@@ -526,22 +523,31 @@ static int ALCcaptureOSS_recordProc(void *ptr)
while(!self->killNow)
{
amt = read(self->fd, self->read_data, self->data_size);
if(amt < 0)
ll_ringbuffer_data_t vec[2];
amt = 0;
if(self->doCapture)
{
ERR("read failed: %s\n", strerror(errno));
ALCcaptureOSS_lock(self);
aluHandleDisconnect(device);
ALCcaptureOSS_unlock(self);
break;
ll_ringbuffer_get_write_vector(self->ring, vec);
if(vec[0].len > 0)
{
amt = read(self->fd, vec[0].buf, vec[0].len*frameSize);
if(amt < 0)
{
ERR("read failed: %s\n", strerror(errno));
ALCcaptureOSS_lock(self);
aluHandleDisconnect(device);
ALCcaptureOSS_unlock(self);
break;
}
ll_ringbuffer_write_advance(self->ring, amt/frameSize);
}
}
if(amt == 0)
{
al_nssleep(1000000);
continue;
}
if(self->doCapture)
WriteRingBuffer(self->ring, self->read_data, amt/frameSize);
}
return 0;
@@ -657,7 +663,7 @@ static ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name)
return ALC_INVALID_VALUE;
}
self->ring = CreateRingBuffer(frameSize, device->UpdateSize * device->NumUpdates);
self->ring = ll_ringbuffer_create(device->UpdateSize*device->NumUpdates + 1, frameSize);
if(!self->ring)
{
ERR("Ring buffer create failed\n");
@@ -666,13 +672,11 @@ static ALCenum ALCcaptureOSS_open(ALCcaptureOSS *self, const ALCchar *name)
return ALC_OUT_OF_MEMORY;
}
self->data_size = info.fragsize;
self->read_data = calloc(1, self->data_size);
self->killNow = 0;
if(althrd_create(&self->thread, ALCcaptureOSS_recordProc, self) != althrd_success)
{
device->ExtraData = NULL;
ll_ringbuffer_free(self->ring);
self->ring = NULL;
close(self->fd);
self->fd = -1;
return ALC_OUT_OF_MEMORY;
@@ -693,11 +697,8 @@ static void ALCcaptureOSS_close(ALCcaptureOSS *self)
close(self->fd);
self->fd = -1;
DestroyRingBuffer(self->ring);
ll_ringbuffer_free(self->ring);
self->ring = NULL;
free(self->read_data);
self->read_data = NULL;
}
static ALCboolean ALCcaptureOSS_start(ALCcaptureOSS *self)
@@ -713,13 +714,13 @@ static void ALCcaptureOSS_stop(ALCcaptureOSS *self)
static ALCenum ALCcaptureOSS_captureSamples(ALCcaptureOSS *self, ALCvoid *buffer, ALCuint samples)
{
ReadRingBuffer(self->ring, buffer, samples);
ll_ringbuffer_read(self->ring, buffer, samples);
return ALC_NO_ERROR;
}
static ALCuint ALCcaptureOSS_availableSamples(ALCcaptureOSS *self)
{
return RingBufferSize(self->ring);
return ll_ringbuffer_read_space(self->ring);
}
+9 -9
View File
@@ -430,7 +430,7 @@ typedef struct ALCwinmmCapture {
HWAVEIN InHdl;
RingBuffer *Ring;
ll_ringbuffer_t *Ring;
WAVEFORMATEX Format;
@@ -514,8 +514,9 @@ static int ALCwinmmCapture_captureProc(void *arg)
break;
WaveHdr = ((WAVEHDR*)msg.lParam);
WriteRingBuffer(self->Ring, (ALubyte*)WaveHdr->lpData,
WaveHdr->dwBytesRecorded/self->Format.nBlockAlign);
ll_ringbuffer_write(self->Ring, WaveHdr->lpData,
WaveHdr->dwBytesRecorded / self->Format.nBlockAlign
);
// Send buffer back to capture more data
waveInAddBuffer(self->InHdl, WaveHdr, sizeof(WAVEHDR));
@@ -603,7 +604,7 @@ static ALCenum ALCwinmmCapture_open(ALCwinmmCapture *self, const ALCchar *name)
if(CapturedDataSize < (self->Format.nSamplesPerSec / 10))
CapturedDataSize = self->Format.nSamplesPerSec / 10;
self->Ring = CreateRingBuffer(self->Format.nBlockAlign, CapturedDataSize);
self->Ring = ll_ringbuffer_create(CapturedDataSize+1, self->Format.nBlockAlign);
if(!self->Ring) goto failure;
InitRef(&self->WaveBuffersCommitted, 0);
@@ -644,8 +645,7 @@ failure:
free(BufferData);
}
if(self->Ring)
DestroyRingBuffer(self->Ring);
ll_ringbuffer_free(self->Ring);
self->Ring = NULL;
if(self->InHdl)
@@ -678,7 +678,7 @@ static void ALCwinmmCapture_close(ALCwinmmCapture *self)
}
free(buffer);
DestroyRingBuffer(self->Ring);
ll_ringbuffer_free(self->Ring);
self->Ring = NULL;
// Close the Wave device
@@ -699,13 +699,13 @@ static void ALCwinmmCapture_stop(ALCwinmmCapture *self)
static ALCenum ALCwinmmCapture_captureSamples(ALCwinmmCapture *self, ALCvoid *buffer, ALCuint samples)
{
ReadRingBuffer(self->Ring, buffer, samples);
ll_ringbuffer_read(self->Ring, buffer, samples);
return ALC_NO_ERROR;
}
static ALCuint ALCwinmmCapture_availableSamples(ALCwinmmCapture *self)
{
return RingBufferSize(self->Ring);
return ll_ringbuffer_read_space(self->Ring);
}