qahw_api:test: Add changes to register for qas callback

Add changes to get notification to client when qas died.
Also fix memory leak issues.

Change-Id: I95a9e91bb6b288b7ce134a60fe650ed3afa78be4
diff --git a/qahw_api/test/Android.mk b/qahw_api/test/Android.mk
index 887f416..ec4e698 100644
--- a/qahw_api/test/Android.mk
+++ b/qahw_api/test/Android.mk
@@ -8,6 +8,7 @@
 LOCAL_MODULE := hal_play_test
 
 hal-play-inc     = $(TARGET_OUT_HEADERS)/mm-audio/qahw_api/inc
+hal-play-inc    += $(TARGET_OUT_HEADERS)/mm-audio/qahw/inc
 hal-play-inc    += external/tinyalsa/include
 
 LOCAL_CFLAGS += -Wall -Werror -Wno-sign-compare
@@ -15,6 +16,7 @@
 LOCAL_SHARED_LIBRARIES := \
     libaudioutils\
     libqahw \
+    libqahwwrapper \
     libutils \
     libcutils
 
diff --git a/qahw_api/test/Makefile.am b/qahw_api/test/Makefile.am
index b4bbea7..2ca177e 100644
--- a/qahw_api/test/Makefile.am
+++ b/qahw_api/test/Makefile.am
@@ -1,34 +1,35 @@
 bin_PROGRAMS = hal_play_test
 
-PLAY_CPPFLAGS = -Wno-sign-compare -Werror
-PLAY_CPPFLAGS += -D__unused=__attribute__\(\(__unused__\)\)
+PLAY_CFLAGS = -Wno-sign-compare -Werror
+PLAY_CFLAGS += -D__unused=__attribute__\(\(__unused__\)\)
 PLAY_INCLUDES = -I $(top_srcdir)/qahw_api/inc
+PLAY_INCLUDES += -I $(top_srcdir)/qahw/inc
 
 hal_play_test_SOURCES = qahw_playback_test.c \
                         qahw_effect_test.c
 
 hal_play_test_LDADD = -lutils ../libqahw.la
+AM_CFLAGS = -I $(PKG_CONFIG_SYSROOT_DIR)/usr/include/mm-audio/qahw/inc
 
 if QAP
-AM_CFLAGS = -DQAP
+AM_CFLAGS += -DQAP
 AM_CFLAGS += -I ${WORKSPACE}/audio/mm-audio/qap_wrapper/inc/
 hal_play_test_SOURCES += qap_wrapper_extn.c
 hal_play_test_LDADD += -lqap_wrapper
 endif
 
-hal_play_test_CPPFLAGS = $(PLAY_CPPFLAGS) $(PLAY_INCLUDES)
-hal_play_test_CFLAGS = $(AM_CFLAGS)
-hal_play_test_CFLAGS = -DLINUX_ENABLED
+hal_play_test_CFLAGS = $(PLAY_CFLAGS) $(PLAY_INCLUDES)
+hal_play_test_CFLAGS += $(AM_CFLAGS) -DLINUX_ENABLED
 
 bin_PROGRAMS += hal_rec_test
 
-REC_CPPFLAGS = $(PLAY_CPPFLAGS) -Dnullptr=NULL
+REC_CFLAGS = $(PLAY_CFLAGS) -Dnullptr=NULL
 REC_INCLUDES = $(PLAY_INCLUDES)
 
 hal_rec_test_SOURCES = qahw_multi_record_test.c
-hal_rec_test_CPPFLAGS = -Dstrlcat=g_strlcat $(GLIB_CFLAGS) -include glib.h
-hal_rec_test_CPPFLAGS += $(REC_CPPFLAGS) $(REC_INCLUDES)
-hal_play_test_CFLAGS += $(AM_CFLAGS)
+hal_rec_test_CFLAGS = -Dstrlcat=g_strlcat $(GLIB_CFLAGS) -include glib.h
+hal_rec_test_CFLAGS += $(REC_CFLAGS) $(REC_INCLUDES)
+hal_rec_test_CFLAGS += $(AM_CFLAGS)
 hal_rec_test_LDADD = -lutils ../libqahw.la $(GLIB_LIBS)
 
 bin_PROGRAMS += trans_loopback_test
@@ -37,6 +38,6 @@
 
 trans_loopback_test_SOURCES = trans_loopback_test.c
 
-trans_loopback_test_CFLAGS  = $(CFLAGS) -Wno-sign-compare -Werror
+trans_loopback_test_CFLAGS  = $(AM_CFLAGS) -Wno-sign-compare -Werror
 trans_loopback_test_CFLAGS  += $(trans_loopback_test_INCLUDES)
 trans_loopback_test_LDADD = -llog  -lutils ../libqahw.la -lcutils
diff --git a/qahw_api/test/qahw_multi_record_test.c b/qahw_api/test/qahw_multi_record_test.c
index 5b07b0f..d618101 100644
--- a/qahw_api/test/qahw_multi_record_test.c
+++ b/qahw_api/test/qahw_multi_record_test.c
@@ -90,6 +90,7 @@
 int sourcetrack_done = 0;
 static pthread_mutex_t sourcetrack_lock;
 struct qahw_sound_focus_param sound_focus_data;
+void *context = NULL;
 
 static bool request_wake_lock(bool wakelock_acquired, bool enable)
 {
@@ -130,7 +131,6 @@
    stop_record = true;
 }
 
-
 void read_soundfocus_param(void)
 {
     uint16_t start_angle[4] = {0};
@@ -249,7 +249,9 @@
 {
   int rc = 0, ret = 0, count = 0;
   FILE *fdLatencyNode = nullptr;
-  struct timespec tsColdI, tsColdF, tsCont;
+  struct timespec tsColdI = { 0, 0 };
+  struct timespec tsColdF = { 0, 0 };
+  struct timespec tsCont = { 0, 0 };
   uint64_t tCold, tCont, tsec, tusec;
   char latencyBuf[200] = {0};
   time_t start_time = time(0);
@@ -352,6 +354,7 @@
       fprintf(log_file, "File open failed \n");
       if (log_file != stdout)
           fprintf(stdout, "File open failed \n");
+      free(buffer);
       test_end();
       pthread_exit(0);
   }
@@ -442,6 +445,7 @@
   hdr.riff_sz = data_sz + 44 - 8;
   fseek(fd, 0, SEEK_SET);
   fwrite(&hdr, 1, sizeof(hdr), fd);
+  free(buffer);
   fclose(fd);
 
   /* capture latency kpis if required */
@@ -450,7 +454,8 @@
               tsColdF.tv_nsec/1000000 - tsColdI.tv_nsec/1000000;
 
       fread((void *) latencyBuf, 100, 1, fdLatencyNode);
-      fclose(fdLatencyNode);
+      if (fdLatencyNode)
+          fclose(fdLatencyNode);
       sscanf(latencyBuf, " %llu,%llu", &tsec, &tusec);
       tCont = ((uint64_t)tsCont.tv_sec)*1000 - tsec*1000 + ((uint64_t)tsCont.tv_nsec)/1000000 - tusec/1000;
       if (log_file != stdout) {
@@ -583,6 +588,12 @@
     printf("                                               For mono channel 16kHz rate for 30seconds\n\n");
 }
 
+static void qti_audio_server_death_notify_cb(void *ctxt) {
+    fprintf(log_file, "qas died\n");
+    fprintf(stderr, "qas died\n");
+    stop_record = true;
+}
+
 int main(int argc, char* argv[]) {
     int max_recordings_requested = 0, status = 0;
     int thread_active[MAX_RECORD_SESSIONS] = {0};
@@ -675,6 +686,8 @@
                 break;
          }
     }
+    fprintf(log_file, "registering qas callback");
+    qahw_register_qas_death_notify_cb((audio_error_callback)qti_audio_server_death_notify_cb, context);
 
     wakelock_acquired = request_wake_lock(wakelock_acquired, true);
     qahw_mod_handle = qahw_load_module(mod_name);
diff --git a/qahw_api/test/qahw_playback_test.c b/qahw_api/test/qahw_playback_test.c
index fc55f83..fceff8b 100644
--- a/qahw_api/test/qahw_playback_test.c
+++ b/qahw_api/test/qahw_playback_test.c
@@ -54,6 +54,10 @@
 #define DTSHD_CHUNK_STREAM_KEYWORD "STRMDATA"
 #define DTSHD_META_KEYWORD_SIZE 8 /*in bytes */
 
+#ifndef AUDIO_OUTPUT_FLAG_MAIN
+#define AUDIO_OUTPUT_FLAG_MAIN 0x8000000
+#endif
+
 static ssize_t get_bytes_to_read(FILE* file, int filetype);
 static void init_streams(void);
 int pthread_cancel(pthread_t thread);
@@ -351,7 +355,7 @@
     struct proxy_data* params = (struct proxy_data*) data;
     qahw_module_handle_t *qahw_mod_handle = params->acp.qahw_mod_handle;
     qahw_in_buffer_t in_buf;
-    char *buffer;
+    char *buffer = NULL;
     int rc = 0;
     int bytes_to_read, bytes_written = 0, bytes_wrote = 0;
     FILE *fp = NULL;
@@ -378,6 +382,7 @@
         if ((fp = fopen(params->acp.file_name,"w"))== NULL) {
             fprintf(log_file, "Cannot open file to dump proxy data\n");
             fprintf(stderr, "Cannot open file to dump proxy data\n");
+            free(buffer);
             pthread_exit(0);
         }
         else {
@@ -420,6 +425,7 @@
             fprintf(stderr, "could not close input stream %d \n", rc);
         }
         fprintf(log_file, "pcm data saved to file %s", params->acp.file_name);
+        free(buffer);
     }
     return 0;
 }
@@ -463,7 +469,7 @@
     return NULL;
 }
 
-static int is_eof(stream_config *stream) {
+static int __unused is_eof (stream_config *stream) {
     if (stream->filename) {
         if (feof(stream->file_stream)) {
             fprintf(log_file, "stream %d: error in fread, error %d\n", stream->stream_index, ferror(stream->file_stream));
@@ -505,7 +511,7 @@
     ret = qahw_out_write(out_handle, &out_buf);
     if (ret < 0) {
         fprintf(log_file, "stream %d: writing data to hal failed (ret = %zd)\n", stream_params->stream_index, ret);
-    } else if (ret != bytes) {
+    } else if ((ret != bytes) && (!stop_playback)) {
         fprintf(log_file, "stream %d: provided bytes %zd, written bytes %d\n",stream_params->stream_index, bytes, ret);
         fprintf(log_file, "stream %d: waiting for event write ready\n", stream_params->stream_index);
         pthread_cond_wait(&stream_params->write_cond, &stream_params->write_lock);
@@ -516,7 +522,7 @@
     return ret;
 }
 
-static bool is_assoc_active()
+static bool __unused is_assoc_active()
 {
     int i = 0;
     bool is_assoc_active = false;
@@ -530,7 +536,7 @@
     return is_assoc_active;
 }
 
-static int get_assoc_index()
+static int __unused get_assoc_index()
 {
     int i = 0;
 
@@ -1132,6 +1138,7 @@
     if (data == NULL) {
         fprintf(log_file, "calloc failed!!\n");
         fprintf(stderr, "calloc failed!!\n");
+        fclose(fd_latency_node);
         return -ENOMEM;
     }
 
@@ -1144,7 +1151,8 @@
             if (ret) {
                 fprintf(log_file, "error(%d) fetching start time for cold latency", ret);
                 fprintf(stderr, "error(%d) fetching start time for cold latency", ret);
-                return -1;
+                rc = -1;
+                goto exit;
             }
         } else if (count == 16) {
             int *d = (int *)data;
@@ -1153,7 +1161,8 @@
             if (ret) {
                 fprintf(log_file, "error(%d) fetching start time for continuous latency", ret);
                 fprintf(stderr, "error(%d) fetching start time for continuous latency", ret);
-                return -1;
+                rc = -1;
+                goto exit;
             }
         }
 
@@ -1179,6 +1188,9 @@
     fprintf(log_file, "\n values from debug node %s\n", latency_buf);
     fprintf(log_file, " cold latency %llums, continuous latency %llums,\n", tcold, tcont);
     fprintf(log_file, " **Note: please add DSP Pipe/PP latency numbers to this, for final latency values\n");
+exit:
+    fclose(fd_latency_node);
+    free(data);
     return rc;
 }
 
@@ -1221,7 +1233,7 @@
 
 void parse_aptx_dec_bt_addr(char *value, struct qahw_aptx_dec_param *aptx_cfg)
 {
-    int ba[6];
+    int ba[6] = {0, 0, 0, 0, 0, 0};
     char *str, *tok;
     uint32_t addr[3];
     int i = 0;
@@ -1693,7 +1705,7 @@
                 ret = fread(&read_chunk_size, 1, DTSHD_META_KEYWORD_SIZE, file);
                 chunk_size = convert_BE_to_LE(read_chunk_size);
                 if (ret != DTSHD_META_KEYWORD_SIZE) {
-                    fprintf(stderr,"%s %d file read error ret %\n",
+                    fprintf(stderr,"%s %d file read error ret %d \n",
                             __func__, __LINE__, ret);
                     file_read_size = -EINVAL;
                     break;
@@ -1836,6 +1848,7 @@
     char *init_ptr = NULL;
     char *token = NULL;
     char *saveptr = NULL;
+    int rc = 0;
 
     if (NULL == channel_map)
         return -EINVAL;
@@ -1849,20 +1862,24 @@
         init_ptr = token_string;
         token = strtok_r(token_string, ",", &saveptr);
         int index = 0;
-        if (NULL == token)
-            return -EINVAL;
+        if (NULL == token) {
+            rc = -EINVAL;
+            goto exit;
+        }
         else
             channel_map[index++] = get_channel_mask_for_name(token);
 
         while(NULL !=(token = strtok_r(NULL,",",&saveptr)))
             channel_map[index++] = get_channel_mask_for_name(token);
 
-        free(init_ptr);
-        init_ptr = NULL;
-        token_string = NULL;
+        goto exit;
     } else
         return -EINVAL;
-    return 0;
+exit:
+    free(init_ptr);
+    init_ptr = NULL;
+    token_string = NULL;
+    return rc;
 }
 
 int extract_mixer_coeffs(qahw_mix_matrix_params_t * mm_params, const char * arg_string){
@@ -1872,7 +1889,7 @@
     char *token = NULL;
     char *saveptr = NULL;
     int i = 0;
-    int j = 0;
+    int j = 0, rc = 0;
 
     if (NULL == mm_params)
         return -EINVAL;
@@ -1885,9 +1902,10 @@
     if(token_string != NULL) {
         init_ptr = token_string;
         token = strtok_r(token_string, ",", &saveptr);
-        int index = 0;
-        if (NULL == token)
-            return -EINVAL;
+        if (NULL == token) {
+            rc = -EINVAL;
+            goto exit;
+        }
         else {
             mm_params->mixer_coeffs[i][j] = atof(token);
             j++;
@@ -1902,11 +1920,14 @@
                 break;
             mm_params->mixer_coeffs[i][j++] = atof(token);
         }
-        free(init_ptr);
-        init_ptr = NULL;
-        token_string = NULL;
+        goto exit;
     } else
         return -EINVAL;
+exit:
+    free(init_ptr);
+    init_ptr = NULL;
+    token_string = NULL;
+    return rc;
 }
 
 #ifdef QAP
@@ -1956,6 +1977,16 @@
 }
 #endif
 
+static void qti_audio_server_death_notify_cb(void *ctxt) {
+    fprintf(log_file, "qas died\n");
+    fprintf(stderr, "qas died\n");
+
+    stream_config *s_params = (stream_config*) ctxt;
+    pthread_cond_signal(&s_params->write_cond);
+    pthread_cond_signal(&s_params->drain_cond);
+    stop_playback = true;
+}
+
 int main(int argc, char* argv[]) {
     char *ba = NULL;
     qahw_param_payload payload;
@@ -2115,14 +2146,14 @@
                 get_kvpairs_string(optarg, "O", output_ch_map);
                 get_kvpairs_string(optarg, "M", mixer_coeff);
 
-                extract_channel_mapping(stream_param[i].mm_params_downmix.input_channel_map, input_ch_map);
+                extract_channel_mapping((uint16_t *)(stream_param[i].mm_params_downmix.input_channel_map), input_ch_map);
                 stream_param[i].mm_params_downmix.has_input_channel_map = 1;
                 fprintf(log_file, "\ndownmix Input channel mapping: ");
                 for (iter_i= 0; iter_i < stream_param[i].mm_params_downmix.num_input_channels; iter_i++) {
                     fprintf(log_file, "0x%x, ", stream_param[i].mm_params_downmix.input_channel_map[iter_i]);
                 }
 
-                extract_channel_mapping(stream_param[i].mm_params_downmix.output_channel_map, output_ch_map);
+                extract_channel_mapping((uint16_t *)(stream_param[i].mm_params_downmix.output_channel_map), output_ch_map);
                 stream_param[i].mm_params_downmix.has_output_channel_map = 1;
                 fprintf(log_file, "\ndownmix Output channel mapping: ");
                 for (iter_i = 0; iter_i < stream_param[i].mm_params_downmix.num_output_channels; iter_i++)
@@ -2150,14 +2181,14 @@
                 get_kvpairs_string(optarg, "O", output_ch_map);
                 get_kvpairs_string(optarg, "M", mixer_coeff);
 
-                extract_channel_mapping(stream_param[i].mm_params_pan_scale.input_channel_map, input_ch_map);
+                extract_channel_mapping((uint16_t *)(stream_param[i].mm_params_pan_scale.input_channel_map), input_ch_map);
                 stream_param[i].mm_params_pan_scale.has_input_channel_map = 1;
                 fprintf(log_file, "\n pan_sclae Input channel mapping: ");
                 for (iter_i= 0; iter_i < stream_param[i].mm_params_pan_scale.num_input_channels; iter_i++) {
                     fprintf(log_file, "0x%x, ", stream_param[i].mm_params_pan_scale.input_channel_map[iter_i]);
                 }
 
-                extract_channel_mapping(stream_param[i].mm_params_pan_scale.output_channel_map, output_ch_map);
+                extract_channel_mapping((uint16_t *)(stream_param[i].mm_params_pan_scale.output_channel_map), output_ch_map);
                 stream_param[i].mm_params_pan_scale.has_output_channel_map = 1;
                 fprintf(log_file, "\n pan_scale Output channel mapping: ");
                 for (iter_i = 0; iter_i < stream_param[i].mm_params_pan_scale.num_output_channels; iter_i++)
@@ -2313,6 +2344,8 @@
 
         }
     }
+    fprintf(log_file, "registering qas callback");
+    qahw_register_qas_death_notify_cb((audio_error_callback)qti_audio_server_death_notify_cb, &stream_param);
 
     wakelock_acquired = request_wake_lock(wakelock_acquired, true);
     num_of_streams = i+1;