[Audio_test]修复了回调方法的实现

1.修复了回调方法的实现
2.修复了录音功能的bug
3.修改了程序选项

Change-Id: Ib8688ddb0966c2d6c40422fac7d7f82a52ed76c3
diff --git a/mbtk/mbtk_lib/src/mbtk_audio.c b/mbtk/mbtk_lib/src/mbtk_audio.c
index ff58fe9..f026769 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio.c
@@ -59,6 +59,30 @@
     PARAM_INT_POLICY_3,
     PARAM_INT_POLICY_MAX,
 } PARAM_INT_POLICY;
+
+#define FAILURE -1
+#define ID_RIFF 0x46464952
+#define ID_WAVE 0x45564157
+#define ID_FMT  0x20746d66
+#define ID_DATA 0x61746164
+#define FORMAT_PCM 1
+
+struct wav_header {
+	uint32_t riff_id;
+	uint32_t riff_sz;
+	uint32_t riff_fmt;
+	uint32_t fmt_id;
+	uint32_t fmt_sz;
+	uint16_t audio_format;
+	uint16_t num_channels;
+	uint32_t sample_rate;
+	uint32_t byte_rate;       /* sample_rate * num_channels * bps / 8 */
+	uint16_t block_align;     /* num_channels * bps / 8 */
+	uint16_t bits_per_sample;
+	uint32_t data_id;
+	uint32_t data_sz;
+};
+
 const struct blobmsg_policy int_policy[] ={
 	[PARAM_INT_POLICY_0] = {
 		.name = "param0",
@@ -105,6 +129,36 @@
 #define AUDIO_FILE_DIR	"/data"
 #define MBTK_AUD_DEMO_WAV "/data/demo.wav"
 
+int MBTK_wav_pcm16Le_set(int fd)
+{
+    struct wav_header hdr;
+
+    if (fd <= 0)
+        return -1;
+
+    memset(&hdr, 0, sizeof(struct wav_header));
+
+    hdr.riff_id = ID_RIFF;
+    hdr.riff_fmt = ID_WAVE;
+    hdr.fmt_id = ID_FMT;
+    hdr.fmt_sz = 16;
+    hdr.audio_format = FORMAT_PCM;
+    hdr.num_channels = 1;
+    hdr.sample_rate = 8000;
+    hdr.bits_per_sample = 16;
+    hdr.byte_rate = (8000 * 1 * hdr.bits_per_sample) / 8;
+    hdr.block_align = (hdr.bits_per_sample * 1) / 8;
+    hdr.data_id = ID_DATA;
+    hdr.data_sz = 0;
+
+    hdr.riff_sz = hdr.data_sz + 44 - 8;
+    if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        return -1;
+    }
+
+    return 0;
+}
+
 int create_audio_dir(const char *dirname)
 {
     DIR *p_dir;
@@ -199,15 +253,25 @@
         goto err;
     }
 
-    if(-1 == mbtk_audio_record(record_hdl, record_cb_func, NULL))
+    if(0 == MBTK_wav_pcm16Le_set(record_fd))// 设置格式,否则音频播放不了
     {
-        printf("file write error\n");
+        if( 0 != mbtk_audio_record(record_hdl, record_cb_func, NULL))
+        {
+            printf("file write error\n");
+            close(record_fd);
+            record_fd = 0;
+        }
+    }
+    else
+    {
+        printf("arec set file header error\n");
         close(record_fd);
         record_fd = 0;
     }
-    sleep(10);
+
+    sleep(10);// 录音 10S
 err:
-    mbtk_audio_close(record_hdl);
+    mbtk_audio_close(record_hdl);// 关闭录音
     record_hdl = NULL;
     if(record_fd > 0)
     {
diff --git a/mbtk/mbtk_lib/src/mbtk_audio_alsa.c b/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
index 83d2585..d654c6a 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
@@ -79,12 +79,6 @@
     void *cb_data;
 };
 
-struct player_cb_s
-{
-    mbtk_audio_player_cb_func _cb;
-    void *cb_data;
-};
-
 static struct mopen_audio_t *internal_hdl = NULL;
 
 
@@ -306,6 +300,7 @@
         {
             first_set = 1;
             mbtk_audio_set_status(hdl, AUDIO_RUNNING);
+            audio_play_cb(pcxt, AUD_PLAYER_RESUME);
         }
 
         pthread_mutex_lock(&pcxt->_stream_mutex);
@@ -316,7 +311,7 @@
         if(ret < pcxt->pcm_packet_size)
             printf("pcm %d - %d\n", pcxt->pipe_data, ret);
 
-        rc = pcxt->stream_out->write(pcxt->stream_out, data, ret);
+        rc = pcxt->stream_out->write(pcxt->stream_out, data, bufsize);
         if (rc < 0) {
             printf("%s: error writing (child).\n", __FUNCTION__);
             break;
@@ -335,7 +330,7 @@
         /* printf("close pcxt->fd!\n"); */
     }
     if (audio_play_cb)
-        audio_play_cb(pcxt, 5);
+        audio_play_cb(pcxt, AUD_PLAYER_FINISHED);
     pcxt->pid = 0;
 //    mbtk_audio_set_status(hdl, AUDIO_STOP);
     mbtk_audio_set_status(hdl, AUDIO_OPEN);
@@ -365,7 +360,7 @@
     }
     if (pcxt->pid == 0) {
         if (audio_play_cb)
-            audio_play_cb(pcxt, 0);
+            audio_play_cb(pcxt, AUD_PLAYER_START);
         mbtk_audio_set_status(pcxt, AUDIO_RUNNING);
         ret = pthread_create(&pcxt->pid, NULL, mbtk_play_pthread, hdl);
         if (ret != 0)
@@ -625,7 +620,7 @@
 }
 
 
-int mbtk_audio_play_file(void *dev_hdl, int file_fd, int offset, mbtk_audio_player_cb_func cb_func)
+int mbtk_audio_play_file(void *dev_hdl, int file_fd, int offset)
 {
     unsigned bufsize = 0;
     char *data = NULL;
@@ -635,7 +630,7 @@
     int ret;
 
     struct mopen_audio_t *pcxt = (struct mopen_audio_t *)dev_hdl;
-    struct player_cb_s *_usrData = NULL;
+    _play_callback audio_play_cb = (_play_callback)pcxt->usrData;
     if (NULL == dev_hdl || NULL == internal_hdl)
         return -1;
 
@@ -643,9 +638,6 @@
         return -2;
 
     // file_data_sz = mbtk_wav_pcm16Le_check(file_fd);
-    _usrData = malloc(sizeof(struct player_cb_s));
-    _usrData->_cb = cb_func;
-    _usrData->cb_data = NULL;
     bufsize = pcxt->pcm_packet_size;
     data = calloc(1, bufsize);
     if (!data) {
@@ -679,28 +671,29 @@
         while(AUDIO_PAUSE == pcxt->state)
         {
             usleep(80000);
-            _usrData->_cb((int)pcxt, AUD_PLAYER_PAUSE);
+            audio_play_cb(pcxt, AUD_PLAYER_PAUSE);
         }
 
         if ((0 == first_set || AUDIO_RESUME == pcxt->state))
         {
             first_set = 1;
             mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
-            _usrData->_cb((int)pcxt, AUD_PLAYER_RESUME);
+            audio_play_cb(pcxt, AUD_PLAYER_RESUME);
         }
 
         ret = pcxt->stream_out->write(pcxt->stream_out, data, bufsize);
         if (ret < 0) {
             printf("%s: error writing (child).\n", __FUNCTION__);
-            _usrData->_cb((int)pcxt, AUD_PLAYER_ERROR);
+            audio_play_cb(pcxt, AUD_PLAYER_ERROR);
             break;
         } else if (ret < (signed int)pcxt->pcm_packet_size) {
             printf("%s: wrote less than buffer size, rc=%d.\n", __FUNCTION__, ret);
-            _usrData->_cb((int)pcxt, AUD_PLAYER_LESSDATA);
+            audio_play_cb(pcxt, AUD_PLAYER_LESSDATA);
             break;
         }
     }
-    _usrData->_cb((int)pcxt, AUD_PLAYER_FINISHED);
+    if (audio_play_cb)
+        audio_play_cb(pcxt, AUD_PLAYER_FINISHED);
 
     printf("file_data_sz :%d - all_size: %d\n", file_data_sz, all_size);
     mbtk_audio_set_status(dev_hdl, AUDIO_OPEN);