Implemented reference counting inside codec2 management module, allowing for multiple calls of codec_init()
This commit is contained in:
parent
26048b90d1
commit
bf8a257c2b
|
|
@ -31,6 +31,10 @@ extern "C" {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise audio codec manager, allocating data buffers.
|
* Initialise audio codec manager, allocating data buffers.
|
||||||
|
*
|
||||||
|
* This function allows recursive calls. However the number of times this
|
||||||
|
* function is called must be balanced by an equal number of calls to the
|
||||||
|
* terminate() function.
|
||||||
*/
|
*/
|
||||||
void codec_init();
|
void codec_init();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ static struct CODEC2 *codec2;
|
||||||
static stream_sample_t *audioBuf;
|
static stream_sample_t *audioBuf;
|
||||||
static streamId audioStream;
|
static streamId audioStream;
|
||||||
|
|
||||||
|
static uint8_t initCnt = 0;
|
||||||
static bool running;
|
static bool running;
|
||||||
static pthread_t codecThread;
|
static pthread_t codecThread;
|
||||||
static pthread_mutex_t mutex;
|
static pthread_mutex_t mutex;
|
||||||
|
|
@ -58,6 +59,15 @@ static void startThread(void *(*func) (void *));
|
||||||
|
|
||||||
void codec_init()
|
void codec_init()
|
||||||
{
|
{
|
||||||
|
if(initCnt > 0)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
initCnt += 1;
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
initCnt = 1;
|
||||||
running = false;
|
running = false;
|
||||||
readPos = 0;
|
readPos = 0;
|
||||||
writePos = 0;
|
writePos = 0;
|
||||||
|
|
@ -73,6 +83,11 @@ void codec_init()
|
||||||
|
|
||||||
void codec_terminate()
|
void codec_terminate()
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
initCnt -= 1;
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
|
if(initCnt > 0) return;
|
||||||
if(running) codec_stop();
|
if(running) codec_stop();
|
||||||
|
|
||||||
pthread_mutex_destroy(&mutex);
|
pthread_mutex_destroy(&mutex);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue