Add lynq audio API and test.

Change-Id: Icfb293c609b1fe71e03fa8a36c26c9c831b51be9
diff --git a/mbtk/mbtk_audio_lib/src/mbtk_wav.c b/mbtk/mbtk_audio_lib/src/mbtk_wav.c
index dca245c..cfe08a8 100755
--- a/mbtk/mbtk_audio_lib/src/mbtk_wav.c
+++ b/mbtk/mbtk_audio_lib/src/mbtk_wav.c
@@ -9,6 +9,11 @@
 
 #define WAV_PLAY_BUFF 2048
 
+typedef struct {
+    const unsigned char *pcm_data;
+    int data_size;
+} audio_buff_t;
+
 static int wav_play_fd = -1;
 static audio_play_state_enum play_state = AUDIO_PLAY_STATE_STOP;
 static pthread_cond_t play_cond;
@@ -19,6 +24,8 @@
 static struct wav_header recorder_header;
 static uint32 recorver_data_count = 0;
 
+static audio_buff_t audio_buff;
+
 static void audio_play_thread(void *arg)
 {
     int rc, len, frames = 0;
@@ -28,6 +35,7 @@
     pthread_mutex_init(&play_mutex, NULL);
     pthread_cond_init(&play_cond, NULL);
 
+    int data_send = 0;
     while (TRUE) {
         /* Playback loop */
         pthread_mutex_lock(&play_mutex);
@@ -44,16 +52,32 @@
         }
 
         memset(buf, 0x00, sizeof(buf));
-        len = read(wav_play_fd, buf, WAV_PLAY_BUFF);
-        if (len == -1) {
-            LOGE("%s: error reading from file", __FUNCTION__);
-            goto thread_end;
-        }
+        if(wav_play_fd > 0) { // Play file.
+            len = read(wav_play_fd, buf, WAV_PLAY_BUFF);
+            if (len == -1) {
+                LOGE("%s: error reading from file", __FUNCTION__);
+                goto thread_end;
+            }
 
-        if (len == 0) {
-            /* reached EOF */
-            LOGE("%s: Read wav file end.", __FUNCTION__);
-            break;
+            if (len == 0) {
+                /* reached EOF */
+                LOGE("%s: Read wav file end.", __FUNCTION__);
+                break;
+            }
+        } else { // Play buffer.
+            if(data_send >= audio_buff.data_size) {
+                /* reached EOF */
+                LOGE("%s: Read buffer end.", __FUNCTION__);
+                break;
+            }
+            if(audio_buff.data_size - data_send >= WAV_PLAY_BUFF) {
+                memcpy(buf, audio_buff.pcm_data + data_send, WAV_PLAY_BUFF);
+                len = WAV_PLAY_BUFF;
+            } else {
+                memcpy(buf, audio_buff.pcm_data + data_send, audio_buff.data_size - data_send);
+                len = audio_buff.data_size - data_send;
+            }
+            data_send += len;
         }
 
         if((rc = mbtk_audio_pcm_play_data_send(buf, len)) < len) {
@@ -71,8 +95,10 @@
     pthread_cond_destroy(&play_cond);
     mbtk_audio_pcm_play_stop();
 
-    if (close(wav_play_fd))
-        LOGE("%s: error closing file", __FUNCTION__);
+    if(wav_play_fd > 0) {
+        if (close(wav_play_fd))
+            LOGE("%s: error closing file", __FUNCTION__);
+    }
 
     wav_play_fd = -1;
     LOGD("%s: finished pcm playback.", __FUNCTION__);
@@ -204,6 +230,56 @@
     return 0;
 }
 
+int mbtk_audio_wav_stream_play_start(const unsigned char *pcm_data, int data_size, int sample_rate, int num_channels)
+{
+    if(play_state != AUDIO_PLAY_STATE_STOP) {
+        LOGW("Audio is playing...");
+        return -1;
+    }
+
+    //Support 8k/16k & mono wave file
+    if (((sample_rate != 8000) && (sample_rate != 16000))
+            || (num_channels != 1) ) {
+        LOGD("%s: error wave file:sample_rate = %d, num_channels = %d!!",
+                __FUNCTION__,sample_rate, num_channels);
+        return -1;
+    }
+
+    LOGE("%s: success open wave stream, sample_rate = %d, num_channels = %d.",
+            __FUNCTION__,  sample_rate, num_channels);
+
+    if ((8000 == sample_rate) && (1 == num_channels)) {
+        mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_8000);
+    } else if ((16000 == sample_rate) && (1 == num_channels)) {
+        mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_16000);
+    }
+
+    if (mbtk_audio_pcm_play_start()) {
+        LOGE("%s: error opening output device.", __FUNCTION__);
+        return -1;
+    }
+
+    audio_buff.pcm_data = pcm_data;
+    audio_buff.data_size = data_size;
+    LOGD("Start play wav stream...");
+
+    pthread_attr_t thread_attr;
+    pthread_attr_init(&thread_attr);
+    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+    {
+        LOGE("pthread_attr_setdetachstate() fail.");
+        return -1;
+    }
+
+    if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
+        LOGE("%s: error creating thread_play!", __FUNCTION__);
+        return -1;
+    }
+
+    return 0;
+}
+
+
 int mbtk_audio_wav_play_pause()
 {
     int result = 0;
@@ -211,7 +287,7 @@
     if(play_state == AUDIO_PLAY_STATE_RUNNING) {
         play_state = AUDIO_PLAY_STATE_PAUSE;
     } else {
-        result = -1;
+        // result = -1;
         LOGW("Audio state : %d", play_state);
     }
     pthread_mutex_unlock(&play_mutex);
@@ -226,7 +302,7 @@
         play_state = AUDIO_PLAY_STATE_RUNNING;
         pthread_cond_signal(&play_cond);
     } else {
-        result = -1;
+        // result = -1;
         LOGW("Audio state : %d", play_state);
     }
     pthread_mutex_unlock(&play_mutex);
@@ -253,7 +329,7 @@
         LOGD("Play thread exit success.");
     } else {
         pthread_mutex_unlock(&play_mutex);
-        result = -1;
+        // result = -1;
         LOGW("Audio state : %d", play_state);
     }