提交 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 @@
* mod_portaudio.c -- PortAudio Endpoint Module
*
*/
#include <switch.h>
#include "switch.h"
#include <stdio.h>
#include <stdlib.h>
......@@ -47,8 +47,7 @@ static switch_memory_pool_t *module_pool = NULL;
static switch_endpoint_interface_t *channel_endpoint_interface;
#define SAMPLE_TYPE paInt16
//#define SAMPLE_TYPE paFloat32
typedef short SAMPLE;
typedef int16_t SAMPLE;
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)
if (olen == 0) {
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
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.samples = samples;
......@@ -682,7 +686,8 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc
if (globals.audio_stream) {
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;
}
......
......@@ -51,17 +51,19 @@
*/
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct PaUtilRingBuffer {
long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */
long writeIndex; /* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */
long readIndex; /* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */
long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */
long smallMask; /* Used for fitting indices to buffer. */
char *buffer;
} PaUtilRingBuffer;
extern "C"
{
#endif /* __cplusplus */
typedef struct PaUtilRingBuffer
{
long bufferSize; /* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */
long writeIndex; /* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */
long readIndex; /* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */
long bigMask; /* Used for wrapping indices with extra bit to distinguish full/empty. */
long smallMask; /* Used for fitting indices to buffer. */
char *buffer;
}PaUtilRingBuffer;
/** Initialize Ring Buffer.
......@@ -74,13 +76,13 @@ extern "C" {
@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.
@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.
......@@ -88,7 +90,7 @@ extern "C" {
@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.
......@@ -96,7 +98,7 @@ extern "C" {
@return The number of bytes available for reading.
*/
long PaUtil_GetRingBufferReadAvailable(PaUtilRingBuffer * rbuf);
long PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf );
/** Write data to the ring buffer.
......@@ -108,7 +110,7 @@ extern "C" {
@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.
......@@ -120,7 +122,7 @@ extern "C" {
@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.
......@@ -142,7 +144,9 @@ extern "C" {
@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.
......@@ -152,7 +156,7 @@ extern "C" {
@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.
......@@ -174,7 +178,9 @@ extern "C" {
@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.
......@@ -184,9 +190,9 @@ extern "C" {
@return The new position.
*/
long PaUtil_AdvanceRingBufferReadIndex(PaUtilRingBuffer * rbuf, long numBytes);
long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long numBytes );
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* PA_RINGBUFFER_H */
#endif /* __cplusplus */
#endif /* PA_RINGBUFFER_H */
......@@ -49,11 +49,12 @@
#include <string.h>
#include <time.h>
/************************************************************************/
/******** Constants *****************************************************/
/************************************************************************/
#define FRAMES_PER_BUFFER (256)
#define FRAMES_PER_BUFFER (128)
/************************************************************************/
/******** Prototypes ****************************************************/
......@@ -117,17 +118,24 @@ static PaError PABLIO_TermFIFO(PaUtilRingBuffer * rbuf)
* Write data to ring buffer.
* 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;
char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames;
int time = 0;
while (numBytes > 0) {
bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
numBytes -= bytesWritten;
p += bytesWritten;
if (numBytes > 0)
Pa_Sleep(10);
if (numBytes > 0) {
Pa_Sleep(1);
if (++time >= timeout * 2) {
PaUtil_FlushRingBuffer(&aStream->outFIFO);
return 0;
}
}
}
return numFrames;
}
......@@ -136,19 +144,27 @@ long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames)
* Read data from ring buffer.
* 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;
char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames;
int time = 0;
while (numBytes > 0) {
bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, numBytes);
numBytes -= bytesRead;
p += bytesRead;
if (numBytes > 0)
Pa_Sleep(10);
if (numBytes > 0) {
Pa_Sleep(1);
time++;
if (time > timeout * 2) {
PaUtil_FlushRingBuffer(&aStream->inFIFO);
return 0;
}
}
}
//printf("%d\n", time);
return numFrames;
}
......@@ -172,18 +188,20 @@ long GetAudioStreamReadable(PABLIO_Stream * aStream)
return bytesFull / aStream->bytesPerFrame;
}
/************************************************************/
/***********************************************************
static unsigned long RoundUpToNextPowerOf2(unsigned long n)
{
long numBits = 0;
if (((n - 1) & n) == 0)
return n; /* Already Power of two. */
return n;
while (n > 0) {
n = n >> 1;
numBits++;
}
return (1 << numBits);
}
*/
/************************************************************
* Opens a PortAudio stream with default characteristics.
......@@ -194,11 +212,11 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr,
const PaStreamParameters * inputParameters, const PaStreamParameters * outputParameters, double sampleRate,
PaStreamFlags streamFlags)
{
long bytesPerSample;
long bytesPerSample = 2;
PaError err;
PABLIO_Stream *aStream;
long numFrames;
long numBytes;
//long numBytes;
int channels = 1;
/* Allocate PABLIO_Stream structure for caller. */
......@@ -218,12 +236,8 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr,
channels = outputParameters->channelCount;
}
numFrames = 4 * FRAMES_PER_BUFFER;
numFrames = RoundUpToNextPowerOf2(numFrames);
bytesPerSample = 2;
aStream->samplesPerFrame = channels;
aStream->bytesPerFrame = bytesPerSample * aStream->samplesPerFrame;
numFrames = FRAMES_PER_BUFFER;
aStream->bytesPerFrame = bytesPerSample;
/* Initialize Ring Buffers */
......@@ -240,8 +254,8 @@ PaError OpenAudioStream(PABLIO_Stream ** rwblPtr,
}
/* Make Write FIFO appear full initially. */
numBytes = PaUtil_GetRingBufferWriteAvailable(&aStream->outFIFO);
PaUtil_AdvanceRingBufferWriteIndex(&aStream->outFIFO, numBytes);
//numBytes = PaUtil_GetRingBufferWriteAvailable(&aStream->outFIFO);
//PaUtil_AdvanceRingBufferWriteIndex(&aStream->outFIFO, numBytes);
/* Open a PortAudio stream that we will use to communicate with the underlying
......
......@@ -61,7 +61,6 @@ extern "C" {
PaUtilRingBuffer outFIFO;
PaStream *stream;
int bytesPerFrame;
int samplesPerFrame;
} PABLIO_Stream;
/* Values for flags for OpenAudioStream(). */
......@@ -75,13 +74,13 @@ extern "C" {
* Write data to ring buffer.
* 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.
* 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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论