diff --git a/extension/mic-asp-sdk/srcs/mic_asp.c b/extension/mic-asp-sdk/srcs/mic_asp.c index 209c32a3..695c7a45 100644 --- a/extension/mic-asp-sdk/srcs/mic_asp.c +++ b/extension/mic-asp-sdk/srcs/mic_asp.c @@ -10,7 +10,7 @@ static PCMContainer_t g_pcmParam; static apr_aiwrap_t* g_aprHandle; static void* g_libInterface; static pthread_t g_voiceThread; -static int g_isStopVoice; +static int g_isStopVoice = 1; #define MAX_BUF_TIME 128000 #define NUM_CHANNELS 8 @@ -26,19 +26,20 @@ static void* voiceCB(void* arg) { int len; unsigned char* pBufData = (unsigned char*)malloc(BUFFER_SIZE); - while (pBufData && g_isStopVoice) { + while (pBufData) { usleep(1000); - len = alsa_read_tdmin_pcm(&g_pcmParam); - memcpy(pBufData, g_pcmParam.data_buf, g_pcmParam.chunk_bytes); - if (len > 0 && len <= BUFFER_SIZE) { - _apr_aiwrap_write(g_aprHandle, pBufData, len); + if(g_isStopVoice == 0) { + len = alsa_read_tdmin_pcm(&g_pcmParam); + memcpy(pBufData, g_pcmParam.data_buf, g_pcmParam.chunk_bytes); + + if (len > 0 && len <= BUFFER_SIZE) { + _apr_aiwrap_write(g_aprHandle, pBufData, len); + } } } - g_isStopVoice = 2; pthread_detach(pthread_self()); - return NULL; } @@ -86,33 +87,24 @@ int mic_asp_init(MicDataCallback cb) { return -5; } + if (pthread_create(&g_voiceThread, NULL, voiceCB, NULL) == -1) { + return -6; + } + return 0; } int mic_asp_start() { int err = _apr_aiwrap_start(g_aprHandle); - if(err != 0) { - return -1; + if(err == 0) { + g_isStopVoice = 0; } - g_isStopVoice = 0; - - if (pthread_create(&g_voiceThread, NULL, voiceCB, NULL) == -1) { - return -2; - } - - return 0; + return err; } int mic_asp_stop() { g_isStopVoice = 1; - - while(g_isStopVoice != 2) { - usleep(1000); - } - - g_isStopVoice = 0; - return _apr_aiwrap_stop(g_aprHandle); } \ No newline at end of file