提交 4e50738c authored 作者: Anthony Minessale's avatar Anthony Minessale

try to fix latency on portaudio

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5434 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 7dabe248
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
* mod_portaudio.c -- PortAudio Endpoint Module * mod_portaudio.c -- PortAudio Endpoint Module
* *
*/ */
#include <switch.h> #include "switch.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -47,8 +47,7 @@ static switch_memory_pool_t *module_pool = NULL; ...@@ -47,8 +47,7 @@ static switch_memory_pool_t *module_pool = NULL;
static switch_endpoint_interface_t *channel_endpoint_interface; static switch_endpoint_interface_t *channel_endpoint_interface;
#define SAMPLE_TYPE paInt16 #define SAMPLE_TYPE paInt16
//#define SAMPLE_TYPE paFloat32 typedef int16_t SAMPLE;
typedef short SAMPLE;
typedef switch_status_t (*pa_command_t) (char **argv, int argc, switch_stream_handle_t *stream); typedef switch_status_t (*pa_command_t) (char **argv, int argc, switch_stream_handle_t *stream);
...@@ -268,7 +267,7 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan) ...@@ -268,7 +267,7 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
if (olen == 0) { if (olen == 0) {
break; break;
} }
WriteAudioStream(globals.ring_stream, abuf, (long) olen); WriteAudioStream(globals.ring_stream, abuf, (long) olen, globals.read_codec.implementation->microseconds_per_frame / 1000);
} }
} }
} }
...@@ -638,7 +637,12 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch ...@@ -638,7 +637,12 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch
switch_mutex_lock(globals.device_lock); switch_mutex_lock(globals.device_lock);
if ((samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data, globals.read_codec.implementation->samples_per_frame)) != 0) { if ((samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
globals.read_codec.implementation->samples_per_frame,
globals.read_codec.implementation->microseconds_per_frame / 1000)) == 0) {
goto cng;
} else {
globals.read_frame.datalen = samples * 2; globals.read_frame.datalen = samples * 2;
globals.read_frame.samples = samples; globals.read_frame.samples = samples;
...@@ -682,7 +686,8 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc ...@@ -682,7 +686,8 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc
if (globals.audio_stream) { if (globals.audio_stream) {
if (switch_test_flag((&globals), GFLAG_EAR)) { if (switch_test_flag((&globals), GFLAG_EAR)) {
WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE))); WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)),
globals.read_codec.implementation->microseconds_per_frame / 1000);
} }
status = SWITCH_STATUS_SUCCESS; status = SWITCH_STATUS_SUCCESS;
} }
......
...@@ -71,27 +71,16 @@ ...@@ -71,27 +71,16 @@
* *
****************/ ****************/
#if defined(__VIA_HACK__) #if defined(HAVE_LIBKERN_OSATOMIC_H) && (defined(__APPLE__) || defined(__FreeBSD__))
#define NO_BARRIER
#endif
#if defined(NO_BARRIER)
# define PaUtil_FullMemoryBarrier()
# define PaUtil_ReadMemoryBarrier()
# define PaUtil_WriteMemoryBarrier()
#else
#if defined(__APPLE__) //|| defined(__FreeBSD__)
# include <libkern/OSAtomic.h> # include <libkern/OSAtomic.h>
/* Here are the memory barrier functions. Mac OS X and FreeBSD only provide /* Here are the memory barrier functions. Mac OS X and FreeBSD only provide
full memory barriers, so the three types of barriers are the same. */ full memory barriers, so the three types of barriers are the same. */
# define PaUtil_FullMemoryBarrier() OSMemoryBarrier() # define PaUtil_FullMemoryBarrier() OSMemoryBarrier()
# define PaUtil_ReadMemoryBarrier() OSMemoryBarrier() # define PaUtil_ReadMemoryBarrier() OSMemoryBarrier()
# define PaUtil_WriteMemoryBarrier() OSMemoryBarrier() # define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
#elif defined(__GNUC__) #elif defined(__GNUC__)
/* GCC understands volatile __asm__ and "memory" to mean it
/* GCC understands volatile asm and "memory" to mean it * should not reorder memory read/writes */
* should not reorder memory read/writes */
# if defined( __PPC__ ) # if defined( __PPC__ )
# define PaUtil_FullMemoryBarrier() __asm__ volatile("sync":::"memory") # define PaUtil_FullMemoryBarrier() __asm__ volatile("sync":::"memory")
# define PaUtil_ReadMemoryBarrier() __asm__ volatile("sync":::"memory") # define PaUtil_ReadMemoryBarrier() __asm__ volatile("sync":::"memory")
...@@ -101,61 +90,63 @@ ...@@ -101,61 +90,63 @@
# define PaUtil_ReadMemoryBarrier() __asm__ volatile("lfence":::"memory") # define PaUtil_ReadMemoryBarrier() __asm__ volatile("lfence":::"memory")
# define PaUtil_WriteMemoryBarrier() __asm__ volatile("sfence":::"memory") # define PaUtil_WriteMemoryBarrier() __asm__ volatile("sfence":::"memory")
# else # else
# ifdef ALLOW_SMP_DANGERS
# warning Memory barriers not defined on this system or system unknown
# warning For SMP safety, you should fix this.
# define PaUtil_FullMemoryBarrier()
# define PaUtil_ReadMemoryBarrier()
# define PaUtil_WriteMemoryBarrier()
# else
# error Memory barriers are not defined on this system. You can still compile by defining ALLOW_SMP_DANGERS, but SMP safety will not be guaranteed.
# endif
# endif
#else
# ifdef ALLOW_SMP_DANGERS
# warning Memory barriers not defined on this system or system unknown
# warning For SMP safety, you should fix this.
# define PaUtil_FullMemoryBarrier() # define PaUtil_FullMemoryBarrier()
# define PaUtil_ReadMemoryBarrier() # define PaUtil_ReadMemoryBarrier()
# define PaUtil_WriteMemoryBarrier() # define PaUtil_WriteMemoryBarrier()
# else
# error Memory barriers are not defined on this system. You can still compile by defining ALLOW_SMP_DANGERS, but SMP safety will not be guaranteed.
# endif # endif
#elif defined(_MSC_VER)
# include <intrin.h>
# pragma intrinsic(_ReadWriteBarrier)
# pragma intrinsic(_ReadBarrier)
# pragma intrinsic(_WriteBarrier)
# define PaUtil_FullMemoryBarrier() _ReadWriteBarrier()
# define PaUtil_ReadMemoryBarrier() _ReadBarrier()
# define PaUtil_WriteMemoryBarrier() _WriteBarrier()
#else
# define PaUtil_FullMemoryBarrier()
# define PaUtil_ReadMemoryBarrier()
# define PaUtil_WriteMemoryBarrier()
#endif
#endif #endif
/*************************************************************************** /***************************************************************************
* Initialize FIFO. * Initialize FIFO.
* numBytes must be power of 2, returns -1 if not. * numBytes must be power of 2, returns -1 if not.
*/ */
long PaUtil_InitializeRingBuffer(PaUtilRingBuffer * rbuf, long numBytes, void *dataPtr) long PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, long numBytes, void *dataPtr )
{ {
if (((numBytes - 1) & numBytes) != 0) if( ((numBytes-1) & numBytes) != 0) return -1; /* Not Power of two. */
return -1; /* Not Power of two. */ rbuf->bufferSize = numBytes;
rbuf->bufferSize = numBytes; rbuf->buffer = (char *)dataPtr;
rbuf->buffer = (char *) dataPtr; PaUtil_FlushRingBuffer( rbuf );
PaUtil_FlushRingBuffer(rbuf); rbuf->bigMask = (numBytes*2)-1;
rbuf->bigMask = (numBytes * 2) - 1; rbuf->smallMask = (numBytes)-1;
rbuf->smallMask = (numBytes) - 1; return 0;
return 0;
} }
/*************************************************************************** /***************************************************************************
** Return number of bytes available for reading. */ ** Return number of bytes available for reading. */
long PaUtil_GetRingBufferReadAvailable(PaUtilRingBuffer * rbuf) long PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf )
{ {
PaUtil_ReadMemoryBarrier(); PaUtil_ReadMemoryBarrier();
return ((rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask); return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask );
} }
/*************************************************************************** /***************************************************************************
** Return number of bytes available for writing. */ ** Return number of bytes available for writing. */
long PaUtil_GetRingBufferWriteAvailable(PaUtilRingBuffer * rbuf) long PaUtil_GetRingBufferWriteAvailable( PaUtilRingBuffer *rbuf )
{ {
/* Since we are calling PaUtil_GetRingBufferReadAvailable, we don't need an aditional MB */ /* Since we are calling PaUtil_GetRingBufferReadAvailable, we don't need an aditional MB */
return (rbuf->bufferSize - PaUtil_GetRingBufferReadAvailable(rbuf)); return ( rbuf->bufferSize - PaUtil_GetRingBufferReadAvailable(rbuf));
} }
/*************************************************************************** /***************************************************************************
** Clear buffer. Should only be called when buffer is NOT being read. */ ** Clear buffer. Should only be called when buffer is NOT being read. */
void PaUtil_FlushRingBuffer(PaUtilRingBuffer * rbuf) void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf )
{ {
rbuf->writeIndex = rbuf->readIndex = 0; rbuf->writeIndex = rbuf->readIndex = 0;
} }
/*************************************************************************** /***************************************************************************
...@@ -164,38 +155,42 @@ void PaUtil_FlushRingBuffer(PaUtilRingBuffer * rbuf) ...@@ -164,38 +155,42 @@ void PaUtil_FlushRingBuffer(PaUtilRingBuffer * rbuf)
** If non-contiguous, size2 will be the size of second region. ** If non-contiguous, size2 will be the size of second region.
** Returns room available to be written or numBytes, whichever is smaller. ** Returns room available to be written or numBytes, whichever is smaller.
*/ */
long PaUtil_GetRingBufferWriteRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2) long PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2, long *sizePtr2 )
{ {
long index; long index;
long available = PaUtil_GetRingBufferWriteAvailable(rbuf); long available = PaUtil_GetRingBufferWriteAvailable( rbuf );
if (numBytes > available) if( numBytes > available ) numBytes = available;
numBytes = available; /* Check to see if write is not contiguous. */
/* Check to see if write is not contiguous. */ index = rbuf->writeIndex & rbuf->smallMask;
index = rbuf->writeIndex & rbuf->smallMask; if( (index + numBytes) > rbuf->bufferSize )
if ((index + numBytes) > rbuf->bufferSize) { {
/* Write data in two blocks that wrap the buffer. */ /* Write data in two blocks that wrap the buffer. */
long firstHalf = rbuf->bufferSize - index; long firstHalf = rbuf->bufferSize - index;
*dataPtr1 = &rbuf->buffer[index]; *dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = firstHalf; *sizePtr1 = firstHalf;
*dataPtr2 = &rbuf->buffer[0]; *dataPtr2 = &rbuf->buffer[0];
*sizePtr2 = numBytes - firstHalf; *sizePtr2 = numBytes - firstHalf;
} else { }
*dataPtr1 = &rbuf->buffer[index]; else
*sizePtr1 = numBytes; {
*dataPtr2 = NULL; *dataPtr1 = &rbuf->buffer[index];
*sizePtr2 = 0; *sizePtr1 = numBytes;
} *dataPtr2 = NULL;
return numBytes; *sizePtr2 = 0;
}
return numBytes;
} }
/*************************************************************************** /***************************************************************************
*/ */
long PaUtil_AdvanceRingBufferWriteIndex(PaUtilRingBuffer * rbuf, long numBytes) long PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, long numBytes )
{ {
/* we need to ensure that previous writes are seen before we update the write index */ /* we need to ensure that previous writes are seen before we update the write index */
PaUtil_WriteMemoryBarrier(); PaUtil_WriteMemoryBarrier();
return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask; return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask;
} }
/*************************************************************************** /***************************************************************************
...@@ -204,72 +199,81 @@ long PaUtil_AdvanceRingBufferWriteIndex(PaUtilRingBuffer * rbuf, long numBytes) ...@@ -204,72 +199,81 @@ long PaUtil_AdvanceRingBufferWriteIndex(PaUtilRingBuffer * rbuf, long numBytes)
** If non-contiguous, size2 will be the size of second region. ** If non-contiguous, size2 will be the size of second region.
** Returns room available to be written or numBytes, whichever is smaller. ** Returns room available to be written or numBytes, whichever is smaller.
*/ */
long PaUtil_GetRingBufferReadRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2) long PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2, long *sizePtr2 )
{ {
long index; long index;
long available = PaUtil_GetRingBufferReadAvailable(rbuf); long available = PaUtil_GetRingBufferReadAvailable( rbuf );
if (numBytes > available) if( numBytes > available ) numBytes = available;
numBytes = available; /* Check to see if read is not contiguous. */
/* Check to see if read is not contiguous. */ index = rbuf->readIndex & rbuf->smallMask;
index = rbuf->readIndex & rbuf->smallMask; if( (index + numBytes) > rbuf->bufferSize )
if ((index + numBytes) > rbuf->bufferSize) { {
/* Write data in two blocks that wrap the buffer. */ /* Write data in two blocks that wrap the buffer. */
long firstHalf = rbuf->bufferSize - index; long firstHalf = rbuf->bufferSize - index;
*dataPtr1 = &rbuf->buffer[index]; *dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = firstHalf; *sizePtr1 = firstHalf;
*dataPtr2 = &rbuf->buffer[0]; *dataPtr2 = &rbuf->buffer[0];
*sizePtr2 = numBytes - firstHalf; *sizePtr2 = numBytes - firstHalf;
} else { }
*dataPtr1 = &rbuf->buffer[index]; else
*sizePtr1 = numBytes; {
*dataPtr2 = NULL; *dataPtr1 = &rbuf->buffer[index];
*sizePtr2 = 0; *sizePtr1 = numBytes;
} *dataPtr2 = NULL;
return numBytes; *sizePtr2 = 0;
}
return numBytes;
} }
/*************************************************************************** /***************************************************************************
*/ */
long PaUtil_AdvanceRingBufferReadIndex(PaUtilRingBuffer * rbuf, long numBytes) long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long numBytes )
{ {
/* we need to ensure that previous writes are always seen before updating the index. */ /* we need to ensure that previous writes are always seen before updating the index. */
PaUtil_WriteMemoryBarrier(); PaUtil_WriteMemoryBarrier();
return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask; return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask;
} }
/*************************************************************************** /***************************************************************************
** Return bytes written. */ ** Return bytes written. */
long PaUtil_WriteRingBuffer(PaUtilRingBuffer * rbuf, const void *data, long numBytes) long PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, long numBytes )
{ {
long size1, size2, numWritten; long size1, size2, numWritten;
void *data1, *data2; void *data1, *data2;
numWritten = PaUtil_GetRingBufferWriteRegions(rbuf, numBytes, &data1, &size1, &data2, &size2); numWritten = PaUtil_GetRingBufferWriteRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
if (size2 > 0) { if( size2 > 0 )
{
memcpy(data1, data, size1); memcpy( data1, data, size1 );
data = ((char *) data) + size1; data = ((char *)data) + size1;
memcpy(data2, data, size2); memcpy( data2, data, size2 );
} else { }
memcpy(data1, data, size1); else
} {
PaUtil_AdvanceRingBufferWriteIndex(rbuf, numWritten); memcpy( data1, data, size1 );
return numWritten; }
PaUtil_AdvanceRingBufferWriteIndex( rbuf, numWritten );
return numWritten;
} }
/*************************************************************************** /***************************************************************************
** Return bytes read. */ ** Return bytes read. */
long PaUtil_ReadRingBuffer(PaUtilRingBuffer * rbuf, void *data, long numBytes) long PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, long numBytes )
{ {
long size1, size2, numRead; long size1, size2, numRead;
void *data1, *data2; void *data1, *data2;
numRead = PaUtil_GetRingBufferReadRegions(rbuf, numBytes, &data1, &size1, &data2, &size2); numRead = PaUtil_GetRingBufferReadRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
if (size2 > 0) { if( size2 > 0 )
memcpy(data, data1, size1); {
data = ((char *) data) + size1; memcpy( data, data1, size1 );
memcpy(data, data2, size2); data = ((char *)data) + size1;
} else { memcpy( data, data2, size2 );
memcpy(data, data1, size1); }
} else
PaUtil_AdvanceRingBufferReadIndex(rbuf, numRead); {
return numRead; memcpy( data, data1, size1 );
}
PaUtil_AdvanceRingBufferReadIndex( rbuf, numRead );
return numRead;
} }
...@@ -51,17 +51,19 @@ ...@@ -51,17 +51,19 @@
*/ */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
#endif /* __cplusplus */ {
#endif /* __cplusplus */
typedef struct PaUtilRingBuffer {
long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */ typedef struct PaUtilRingBuffer
long writeIndex; /* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */ {
long readIndex; /* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */ long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */
long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */ long writeIndex; /* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */
long smallMask; /* Used for fitting indices to buffer. */ long readIndex; /* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */
char *buffer; long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */
} PaUtilRingBuffer; long smallMask; /* Used for fitting indices to buffer. */
char *buffer;
}PaUtilRingBuffer;
/** Initialize Ring Buffer. /** Initialize Ring Buffer.
...@@ -74,13 +76,13 @@ extern "C" { ...@@ -74,13 +76,13 @@ extern "C" {
@return -1 if numBytes is not a power of 2, otherwise 0. @return -1 if numBytes is not a power of 2, otherwise 0.
*/ */
long PaUtil_InitializeRingBuffer(PaUtilRingBuffer * rbuf, long numBytes, void *dataPtr); long PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, long numBytes, void *dataPtr );
/** Clear buffer. Should only be called when buffer is NOT being read. /** Clear buffer. Should only be called when buffer is NOT being read.
@param rbuf The ring buffer. @param rbuf The ring buffer.
*/ */
void PaUtil_FlushRingBuffer(PaUtilRingBuffer * rbuf); void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf );
/** Retrieve the number of bytes available in the ring buffer for writing. /** Retrieve the number of bytes available in the ring buffer for writing.
...@@ -88,7 +90,7 @@ extern "C" { ...@@ -88,7 +90,7 @@ extern "C" {
@return The number of bytes available for writing. @return The number of bytes available for writing.
*/ */
long PaUtil_GetRingBufferWriteAvailable(PaUtilRingBuffer * rbuf); long PaUtil_GetRingBufferWriteAvailable( PaUtilRingBuffer *rbuf );
/** Retrieve the number of bytes available in the ring buffer for reading. /** Retrieve the number of bytes available in the ring buffer for reading.
...@@ -96,7 +98,7 @@ extern "C" { ...@@ -96,7 +98,7 @@ extern "C" {
@return The number of bytes available for reading. @return The number of bytes available for reading.
*/ */
long PaUtil_GetRingBufferReadAvailable(PaUtilRingBuffer * rbuf); long PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf );
/** Write data to the ring buffer. /** Write data to the ring buffer.
...@@ -108,7 +110,7 @@ extern "C" { ...@@ -108,7 +110,7 @@ extern "C" {
@return The number of bytes written. @return The number of bytes written.
*/ */
long PaUtil_WriteRingBuffer(PaUtilRingBuffer * rbuf, const void *data, long numBytes); long PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, long numBytes );
/** Read data from the ring buffer. /** Read data from the ring buffer.
...@@ -120,7 +122,7 @@ extern "C" { ...@@ -120,7 +122,7 @@ extern "C" {
@return The number of bytes read. @return The number of bytes read.
*/ */
long PaUtil_ReadRingBuffer(PaUtilRingBuffer * rbuf, void *data, long numBytes); long PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, long numBytes );
/** Get address of region(s) to which we can write data. /** Get address of region(s) to which we can write data.
...@@ -142,7 +144,9 @@ extern "C" { ...@@ -142,7 +144,9 @@ extern "C" {
@return The room available to be written or numBytes, whichever is smaller. @return The room available to be written or numBytes, whichever is smaller.
*/ */
long PaUtil_GetRingBufferWriteRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2); long PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2, long *sizePtr2 );
/** Advance the write index to the next location to be written. /** Advance the write index to the next location to be written.
...@@ -152,7 +156,7 @@ extern "C" { ...@@ -152,7 +156,7 @@ extern "C" {
@return The new position. @return The new position.
*/ */
long PaUtil_AdvanceRingBufferWriteIndex(PaUtilRingBuffer * rbuf, long numBytes); long PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, long numBytes );
/** Get address of region(s) from which we can write data. /** Get address of region(s) from which we can write data.
...@@ -174,7 +178,9 @@ extern "C" { ...@@ -174,7 +178,9 @@ extern "C" {
@return The number of bytes available for reading. @return The number of bytes available for reading.
*/ */
long PaUtil_GetRingBufferReadRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2); long PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2, long *sizePtr2 );
/** Advance the read index to the next location to be read. /** Advance the read index to the next location to be read.
...@@ -184,9 +190,9 @@ extern "C" { ...@@ -184,9 +190,9 @@ extern "C" {
@return The new position. @return The new position.
*/ */
long PaUtil_AdvanceRingBufferReadIndex(PaUtilRingBuffer * rbuf, long numBytes); long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long numBytes );
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* PA_RINGBUFFER_H */ #endif /* PA_RINGBUFFER_H */
...@@ -49,11 +49,12 @@ ...@@ -49,11 +49,12 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
/************************************************************************/ /************************************************************************/
/******** Constants *****************************************************/ /******** Constants *****************************************************/
/************************************************************************/ /************************************************************************/
#define FRAMES_PER_BUFFER (256) #define FRAMES_PER_BUFFER (128)
/************************************************************************/ /************************************************************************/
/******** Prototypes ****************************************************/ /******** Prototypes ****************************************************/
...@@ -117,17 +118,24 @@ static PaError PABLIO_TermFIFO(PaUtilRingBuffer * rbuf) ...@@ -117,17 +118,24 @@ static PaError PABLIO_TermFIFO(PaUtilRingBuffer * rbuf)
* Write data to ring buffer. * Write data to ring buffer.
* Will not return until all the data has been written. * Will not return until all the data has been written.
*/ */
long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames) long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, int timeout)
{ {
long bytesWritten; long bytesWritten;
char *p = (char *) data; char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames; long numBytes = aStream->bytesPerFrame * numFrames;
int time = 0;
while (numBytes > 0) { while (numBytes > 0) {
bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes); bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
numBytes -= bytesWritten; numBytes -= bytesWritten;
p += bytesWritten; p += bytesWritten;
if (numBytes > 0) if (numBytes > 0) {
Pa_Sleep(10); Pa_Sleep(1);
if (++time >= timeout * 2) {
PaUtil_FlushRingBuffer(&aStream->outFIFO);
return 0;
}
}
} }
return numFrames; return numFrames;
} }
...@@ -136,19 +144,27 @@ long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames) ...@@ -136,19 +144,27 @@ long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames)
* Read data from ring buffer. * Read data from ring buffer.
* Will not return until all the data has been read. * Will not return until all the data has been read.
*/ */
long ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames) long ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, int timeout)
{ {
long bytesRead; long bytesRead;
char *p = (char *) data; char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames; long numBytes = aStream->bytesPerFrame * numFrames;
int time = 0;
while (numBytes > 0) { while (numBytes > 0) {
bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, numBytes); bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, numBytes);
numBytes -= bytesRead; numBytes -= bytesRead;
p += bytesRead; p += bytesRead;
if (numBytes > 0) if (numBytes > 0) {
Pa_Sleep(10); Pa_Sleep(1);
time++;
if (time > timeout * 2) {
PaUtil_FlushRingBuffer(&aStream->inFIFO);
return 0;
}
}
} }
//printf("%d\n", time);
return numFrames; return numFrames;
} }
...@@ -172,18 +188,20 @@ long GetAudioStreamReadable(PABLIO_Stream * aStream) ...@@ -172,18 +188,20 @@ long GetAudioStreamReadable(PABLIO_Stream * aStream)
return bytesFull / aStream->bytesPerFrame; return bytesFull / aStream->bytesPerFrame;
} }
/************************************************************/ /***********************************************************
static unsigned long RoundUpToNextPowerOf2(unsigned long n) static unsigned long RoundUpToNextPowerOf2(unsigned long n)
{ {
long numBits = 0; long numBits = 0;
if (((n - 1) & n) == 0) if (((n - 1) & n) == 0)
return n; /* Already Power of two. */ return n;
while (n > 0) { while (n > 0) {
n = n >> 1; n = n >> 1;
numBits++; numBits++;
} }
return (1 << numBits); return (1 << numBits);
} }
*/
/************************************************************ /************************************************************
* Opens a PortAudio stream with default characteristics. * Opens a PortAudio stream with default characteristics.
...@@ -194,11 +212,11 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr, ...@@ -194,11 +212,11 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr,
const PaStreamParameters * inputParameters, const PaStreamParameters * outputParameters, double sampleRate, const PaStreamParameters * inputParameters, const PaStreamParameters * outputParameters, double sampleRate,
PaStreamFlags streamFlags) PaStreamFlags streamFlags)
{ {
long bytesPerSample; long bytesPerSample = 2;
PaError err; PaError err;
PABLIO_Stream *aStream; PABLIO_Stream *aStream;
long numFrames; long numFrames;
long numBytes; //long numBytes;
int channels = 1; int channels = 1;
/* Allocate PABLIO_Stream structure for caller. */ /* Allocate PABLIO_Stream structure for caller. */
...@@ -218,12 +236,8 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr, ...@@ -218,12 +236,8 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr,
channels = outputParameters->channelCount; channels = outputParameters->channelCount;
} }
numFrames = 4 * FRAMES_PER_BUFFER; numFrames = FRAMES_PER_BUFFER;
numFrames = RoundUpToNextPowerOf2(numFrames); aStream->bytesPerFrame = bytesPerSample;
bytesPerSample = 2;
aStream->samplesPerFrame = channels;
aStream->bytesPerFrame = bytesPerSample * aStream->samplesPerFrame;
/* Initialize Ring Buffers */ /* Initialize Ring Buffers */
...@@ -240,8 +254,8 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr, ...@@ -240,8 +254,8 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr,
} }
/* Make Write FIFO appear full initially. */ /* Make Write FIFO appear full initially. */
numBytes = PaUtil_GetRingBufferWriteAvailable(&aStream->outFIFO); //numBytes = PaUtil_GetRingBufferWriteAvailable(&aStream->outFIFO);
PaUtil_AdvanceRingBufferWriteIndex(&aStream->outFIFO, numBytes); //PaUtil_AdvanceRingBufferWriteIndex(&aStream->outFIFO, numBytes);
/* Open a PortAudio stream that we will use to communicate with the underlying /* Open a PortAudio stream that we will use to communicate with the underlying
......
...@@ -61,7 +61,6 @@ extern "C" { ...@@ -61,7 +61,6 @@ extern "C" {
PaUtilRingBuffer outFIFO; PaUtilRingBuffer outFIFO;
PaStream *stream; PaStream *stream;
int bytesPerFrame; int bytesPerFrame;
int samplesPerFrame;
} PABLIO_Stream; } PABLIO_Stream;
/* Values for flags for OpenAudioStream(). */ /* Values for flags for OpenAudioStream(). */
...@@ -75,13 +74,13 @@ extern "C" { ...@@ -75,13 +74,13 @@ extern "C" {
* Write data to ring buffer. * Write data to ring buffer.
* Will not return until all the data has been written. * Will not return until all the data has been written.
*/ */
long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames); long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, int timeout);
/************************************************************ /************************************************************
* Read data from ring buffer. * Read data from ring buffer.
* Will not return until all the data has been read. * Will not return until all the data has been read.
*/ */
long ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames); long ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, int timeout);
/************************************************************ /************************************************************
* Return the number of frames that could be written to the stream without * Return the number of frames that could be written to the stream without
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论