Fix audio api.

Change-Id: I9c6f45018ae87cc75c3e7948283f68b1b02a7579
diff --git a/mbtk/libql_lib/src/ql_audio.c b/mbtk/libql_lib/src/ql_audio.c
index 184c48f..41e6325 100755
--- a/mbtk/libql_lib/src/ql_audio.c
+++ b/mbtk/libql_lib/src/ql_audio.c
@@ -1,15 +1,130 @@
 #include "ql/ql_audio.h"
 #include "mbtk_log.h"
-#ifdef MBTK_PLATFORM_ASR1803
-#include "mbtk_audio.h"
 
-static mbtk_audio_handle record_hdl = NULL;
-static mbtk_audio_handle player_hdl = NULL;
-static _cb_onRecorder record_cb_fun = NULL;
-static int Samprate = 8000;
-#else
+typedef enum {
+    AUDIO_PLAY_STATE_STOP,
+    AUDIO_PLAY_STATE_RUNNING,
+    AUDIO_PLAY_STATE_PAUSE
+} audio_play_state_enum;
 
-#endif
+#define AUDIO_HANDLE 1
+#define WAV_PLAY_BUFF 1024
+
+static int sample_rate = 8000;
+static int play_handle = AUDIO_HANDLE;
+static _cb_onPlayer play_cb_func = NULL;
+static _cb_onRecorder recorder_cb_fun = NULL;
+static int is_running = 0;
+static audio_play_state_enum play_state = AUDIO_PLAY_STATE_STOP;
+static int play_exit = 1;
+
+static void recorder_cb_func(void *data, uint32 data_len)
+{
+    if(recorder_cb_fun) {
+        recorder_cb_fun(AUD_RECORDER_START, (unsigned char*)data, data_len);
+    }
+}
+
+static int play_stream(unsigned char* pData, unsigned int length)
+{
+    int rc, len, frames = 0;
+    int result = 0;
+
+    play_state = AUDIO_PLAY_STATE_RUNNING;
+    play_exit = 0;
+    int data_send = 0;
+
+    if(pData && length > 0) {
+        while (play_state != AUDIO_PLAY_STATE_STOP) {
+            if(play_state == AUDIO_PLAY_STATE_RUNNING) {
+                if (data_send >= length) {
+                    LOGE("%s: Read pcm stream end.", __FUNCTION__);
+                    break;
+                }
+
+                if(length - data_send > WAV_PLAY_BUFF) {
+                    len = WAV_PLAY_BUFF;
+                } else {
+                    len = length - data_send;
+                }
+
+                if((rc = mbtk_audio_pcm_play_data_send(pData + data_send, len)) != len) {
+                    LOGE("Send data %d/%d", rc, len);
+                    result = -1;
+                    goto thread_end;
+                }
+
+                data_send += len;
+
+                LOGD("%s: No.%d frame playback", __FUNCTION__, ++frames);
+            } else {
+                usleep(200000);
+            }
+        }
+    } else {
+        result = -1;
+    }
+
+    play_state = AUDIO_PLAY_STATE_STOP;
+
+thread_end:
+    mbtk_audio_pcm_play_stop();
+    play_exit = 1;
+    LOGD("%s: finished pcm playback.", __FUNCTION__);
+    return result;
+}
+
+static int play_by_fd(int fd, int offset)
+{
+    int rc, len, frames = 0;
+    int result = 0;
+    char buf[WAV_PLAY_BUFF];
+
+    play_state = AUDIO_PLAY_STATE_RUNNING;
+    play_exit = 0;
+    if(fd > 0) {
+        if(offset > 0) {
+            lseek(fd, offset, SEEK_SET);
+        }
+        while (play_state != AUDIO_PLAY_STATE_STOP) {
+            if(play_state == AUDIO_PLAY_STATE_RUNNING) {
+                memset(buf, 0x00, sizeof(buf));
+                len = read(fd, buf, WAV_PLAY_BUFF);
+                if (len == -1) {
+                    LOGE("%s: error reading from file", __FUNCTION__);
+                    result = -1;
+                    goto thread_end;
+                }
+
+                if (len == 0) {
+                    /* reached EOF */
+                    LOGE("%s: Read wav file end.", __FUNCTION__);
+                    break;
+                }
+
+                if((rc = mbtk_audio_pcm_play_data_send(buf, len)) < len) {
+                    LOGE("Send data %d/%d", rc, len);
+                    result = -1;
+                    goto thread_end;
+                }
+
+                LOGD("%s: No.%d frame playback", __FUNCTION__, ++frames);
+            } else {
+                usleep(200000);
+            }
+        }
+    } else {
+        result = -1;
+    }
+
+    play_state = AUDIO_PLAY_STATE_STOP;
+
+thread_end:
+    mbtk_audio_pcm_play_stop();
+    play_exit = 1;
+    LOGD("%s: finished pcm playback.", __FUNCTION__);
+    return result;
+}
 
 /*****************************************************************
 * Function:     Ql_AudPlayer_Open
@@ -39,12 +154,13 @@
 *****************************************************************/
 int Ql_AudPlayer_Open(char* device, _cb_onPlayer cb_func)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    player_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_OUT, 1, Samprate, cb_func);
-    return (int)player_hdl;
-#else
-    return 0;
-#endif
+    if(is_running || mbtk_audio_pcm_init()) {
+        return -1;
+    } else {
+        play_cb_func = cb_func;
+        is_running = 1;
+        return play_handle;
+    }
 }
 
 /*========================================================================
@@ -67,11 +183,12 @@
 /*=======================================================================*/
 int Ql_AudPlayer_Play(int hdl, unsigned char* pData, unsigned int length)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    return mbtk_audio_play_stream((void *)hdl, pData, length);
-#else
-    return 0;
-#endif
+    if(!is_running || hdl != play_handle) {
+        LOGE("Handle error : %d", hdl);
+        return -1;
+    }
+
+    return play_stream(pData, length);
 }
 
 /*========================================================================
@@ -99,11 +216,12 @@
 
 int  Ql_AudPlayer_PlayFrmFile(int hdl, int fd, int offset)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    return mbtk_audio_play_file((void *)hdl, fd, offset);
-#else
-    return 0;
-#endif
+    if(!is_running || hdl != play_handle) {
+        LOGE("Handle error : %d", hdl);
+        return -1;
+    }
+
+    return play_by_fd(fd, offset);
 }
 
 //
@@ -115,11 +233,18 @@
 //   Handle received from Ql_AudPlayer_Open().
 int  Ql_AudPlayer_Pause(int hdl)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    return mbtk_audio_pause((void *)hdl);
-#else
+    if(!is_running || hdl != play_handle) {
+        LOGE("Handle error : %d", hdl);
+        return -1;
+    }
+
+    play_state = AUDIO_PLAY_STATE_PAUSE;
+
+    while(!play_exit) {
+        usleep(10000);
+    }
+
     return 0;
-#endif
 }
 
 //
@@ -131,11 +256,17 @@
 //   Handle received from Ql_AudPlayer_Open().
 int  Ql_AudPlayer_Resume(int hdl)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    return mbtk_audio_resume((void *)hdl);
-#else
+    if(!is_running || hdl != play_handle) {
+        LOGE("Handle error : %d", hdl);
+        return -1;
+    }
+
+    play_state = AUDIO_PLAY_STATE_RUNNING;
+
+    while(!play_exit) {
+        usleep(10000);
+    }
     return 0;
-#endif
 }
 
 //
@@ -147,11 +278,16 @@
 //   Handle received from Ql_AudPlayer_Open().
 void Ql_AudPlayer_Stop(int hdl)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    mbtk_audio_stop((void *)hdl);
-#else
+    if(!is_running || hdl != play_handle) {
+        LOGE("Handle error : %d", hdl);
+        return;
+    }
 
-#endif
+    play_state = AUDIO_PLAY_STATE_STOP;
+
+    while(!play_exit) {
+        usleep(10000);
+    }
 }
 
 //
@@ -163,11 +299,18 @@
 //   Handle received from Ql_AudPlayer_Open().
 void Ql_AudPlayer_Close(int hdl)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    mbtk_audio_close((void *)hdl);
-#else
+    if(!is_running || hdl != play_handle) {
+        LOGE("Handle error : %d", hdl);
+        return;
+    }
+    play_state = AUDIO_PLAY_STATE_STOP;
 
-#endif
+    while(!play_exit) {
+        usleep(10000);
+    }
+
+    is_running = 0;
+    mbtk_audio_pcm_deinit();
 }
 
 
@@ -203,14 +346,13 @@
 *****************************************************************/
 int  Ql_AudRecorder_Open(char* device, _cb_onRecorder cb_fun)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    record_hdl = mbtk_audio_open(MBTK_AUTIO_TYPE_IN, 1, 8000, NULL);
-    record_cb_fun = cb_fun;
-    return (int)record_hdl;
-#else
-    return 0;
-
-#endif
+    if(is_running || mbtk_audio_pcm_init()) {
+        return -1;
+    } else {
+        is_running = 1;
+        recorder_cb_fun = cb_fun;
+        return play_handle;
+    }
 }
 
 //
@@ -225,12 +367,12 @@
 //            -1 on failure
 int  Ql_AudRecorder_StartRecord(void)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    return mbtk_audio_record(record_hdl, record_cb_fun, NULL);
-#else
-    return 0;
+    if(!is_running) {
+        LOGE("No open device.");
+        return -1;
+    }
 
-#endif
+    return mbtk_audio_pcm_recorder_start(recorder_cb_func);
 }
 
 //
@@ -240,7 +382,12 @@
 //   Pause recording
 int  Ql_AudRecorder_Pause(void)
 {
-    return 0;
+    if(!is_running) {
+        LOGE("No open device.");
+        return -1;
+    }
+
+    return mbtk_audio_pcm_recorder_pause();
 }
 
 //
@@ -250,7 +397,12 @@
 //   Resume recording
 int  Ql_AudRecorder_Resume(void)
 {
-    return 0;
+    if(!is_running) {
+        LOGE("No open device.");
+        return -1;
+    }
+
+    return mbtk_audio_pcm_recorder_resume();
 }
 
 //
@@ -260,7 +412,12 @@
 //   Stop recording
 void Ql_AudRecorder_Stop(void)
 {
+    if(!is_running) {
+        LOGE("No open device.");
+        return;
+    }
 
+    mbtk_audio_pcm_recorder_stop();
 }
 
 //
@@ -270,13 +427,13 @@
 //   Close recorder, and free the resource
 void Ql_AudRecorder_Close(void)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-    mbtk_audio_close(record_hdl);
-    record_hdl = NULL;
-    record_cb_fun = NULL;
-#else
+    if(!is_running) {
+        LOGE("No open device.");
+        return;
+    }
 
-#endif
+    is_running = 0;
+    mbtk_audio_pcm_deinit();
 }
 
 //
@@ -410,27 +567,8 @@
 
 int Ql_Rxgain_Set(int value)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-	printf("Volume is %d \n",value);
-	int volume =0;
-	int handler = 0;
-
-    if(value < -36 || value > 12)
-    {
-        volume = 0;
-    }
-    else
-    {
-        volume = value;
-    }
-
-    mbtk_audio_ubus_client_init(&handler, dtmf_cb1);
-	mbtk_audio_dsp_set(1, volume);
-    return 0;
-#else
 
     return 0;
-#endif
 }
 
 
@@ -444,49 +582,26 @@
  */
 int Ql_Playback_Samprate_Set(int samprate)
 {
-#ifdef MBTK_PLATFORM_ASR1803
     printf("samprate is %d \n",samprate);
     if(samprate == 1)
     {
-       Samprate = 16000;
+        sample_rate = 16000;
     }
     else{
-        Samprate = 8000;
+        sample_rate = 8000;
     }
 
     return 0;
-#else
-
-    return 0;
-#endif
 }
 
 int Ql_Mp3_To_Wav(const char *wavpath, char *mp3path)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-#ifdef MBTK_MP3_SUPPORT
-    return mbtk_audio_mp3_to_wav(wavpath, mp3path);
-#else
     return 0;
-#endif
-#else
-
-    return 0;
-#endif
 }
 
 int Ql_Mp3_To_Play(char *mp3path, int hdl,int sample_rate)
 {
-#ifdef MBTK_PLATFORM_ASR1803
-#ifdef MBTK_MP3_SUPPORT
-    return mbtk_audio_mp3_to_play(mp3path, hdl, sample_rate);
-#else
     return 0;
-#endif
-#else
-
-    return 0;
-#endif
 }
 
 //add by grady, 2018-6-2