audio-hal: hal_play_test: Add changes to unload qap libs in session close

Change-Id: I8e5b24caff1ff246ebf3a75ad5c941084b99bbb8
diff --git a/qahw_api/test/qahw_playback_test.c b/qahw_api/test/qahw_playback_test.c
index fb69d93..038e13e 100644
--- a/qahw_api/test/qahw_playback_test.c
+++ b/qahw_api/test/qahw_playback_test.c
@@ -2337,7 +2337,6 @@
 
     if (is_qap_session_active(argc, argv, kvp_string)) {
         char *file_name = NULL;
-        char *file_name_tmp = NULL;
         char *cmd_kvp_str[100] = {NULL};
         char *play_list_kvp_str[100] = {NULL};
         int i = 0, j = 0;
diff --git a/qahw_api/test/qap_wrapper_extn.c b/qahw_api/test/qap_wrapper_extn.c
index 5c76d40..0405423 100644
--- a/qahw_api/test/qap_wrapper_extn.c
+++ b/qahw_api/test/qap_wrapper_extn.c
@@ -114,7 +114,8 @@
 double data_callback_st_arr[TIMESTAMP_ARRAY_SIZE];
 bool has_system_input = false;
 char session_kv_pairs[256];
-bool stream_close = false;
+bool primary_stream_close = false;
+uint8_t stream_cnt = 0;
 uint32_t dsp_latency = 0;
 
 static int get_qap_out_config_index_for_id(int32_t out_id)
@@ -320,8 +321,7 @@
     if (tmp_str != NULL) {
         file_str = strstr(kvp_string, ".txt");
         len = file_str - tmp_str;
-        play_list = (char*) malloc(sizeof(char) * (len+4));
-        memset(play_list, '\0', len+4);
+        play_list = (char*) calloc(1, sizeof(char) * (len+4));
         strncpy(play_list, tmp_str+2, len+2);
     }
     return play_list;
@@ -392,13 +392,13 @@
             }
         }
         i++;
-        while (!stream_close) {
+        while (!primary_stream_close) {
             usleep(50000);
             fprintf(stderr, "QAP Stream not closed\n");
         }
         fprintf(stderr, "QAP Stream closed\n");
     } while (i <num_of_streams);
-    if (broad_cast && qap_wrapper_session_active) {
+    if (qap_wrapper_session_active) {
         qap_wrapper_session_close();
         qap_wrapper_session_active = false;
     }
@@ -427,8 +427,7 @@
         if (temp_kvp != NULL) {
             temp_key = strtok_r(temp_kvp, "=", &context2);
             if (!strncmp(key, temp_key, strlen(key))) {
-                kvp = malloc((strlen(token) + 1) * sizeof(char));
-                memset(kvp, 0, strlen(token) + 1);
+                kvp = calloc(1, (strlen(token) + 1) * sizeof(char));
                 strncat(kvp, token, strlen(token));
                 return kvp;
             }
@@ -441,8 +440,7 @@
                 if (temp_kvp != NULL) {
                     temp_key = strtok_r(temp_kvp, "=", &context2);
                     if (!strncmp(key, temp_key, strlen(key))) {
-                        kvp = malloc((strlen(token) + 1) * sizeof(char));
-                        memset(kvp, 0, strlen(token) + 1);
+                        kvp = calloc(1, (strlen(token) + 1) * sizeof(char));
                         strncat(kvp, token, strlen(token));
                         return kvp;
                     }
@@ -488,7 +486,7 @@
             for (i=0; s[i]; s[i]==',' ? i++ : *s++);
 
             temp = i;
-            val = malloc((i + 1)*sizeof(int));
+            val = calloc(1, (i + 1)*sizeof(int));
             i = 0;
             val[i++] = strtol(tempstr2, &endstr, 0);
 
@@ -690,7 +688,7 @@
             fprintf(stderr, "%s::%d: could not close output stream, error - %d\n", __func__, __LINE__, ret);
         qap_out_hdmi_handle = NULL;
     }
-    stream_close = true;
+    primary_stream_close = true;
 }
 
 void qap_wrapper_session_callback(qap_session_handle_t session_handle __unused, void* priv_data __unused, qap_callback_event_t event_id, int size __unused, void *data)
@@ -707,13 +705,14 @@
     switch (event_id) {
         case QAP_CALLBACK_EVENT_EOS:
             ALOGV("%s %d Received Main Input EOS", __func__, __LINE__);
+            stream_cnt --;
             pthread_mutex_lock(&main_eos_lock);
             pthread_cond_signal(&main_eos_cond);
             pthread_mutex_unlock(&main_eos_lock);
 
             ALOGE("%s %d Received Main Input EOS ", __func__, __LINE__);
-            if (!stream_close)
-            close_output_streams();
+            if (!stream_cnt)
+                close_output_streams();
             if (play_list_cnt && input_streams_count) {
                 play_list_cnt--;
                 input_streams_count = 0;
@@ -721,12 +720,15 @@
             break;
         case QAP_CALLBACK_EVENT_EOS_ASSOC:
         case QAP_CALLBACK_EVENT_MAIN_2_EOS:
+            stream_cnt --;
             if (!has_system_input){
                 ALOGV("%s %d Received Secondary Input EOS", __func__, __LINE__);
                 pthread_mutex_lock(&sec_eos_lock);
                 pthread_cond_signal(&sec_eos_cond);
                 pthread_mutex_unlock(&sec_eos_lock);
             }
+            if (!stream_cnt)
+                close_output_streams();
             break;
         case QAP_CALLBACK_EVENT_ERROR:
             break;
@@ -1162,7 +1164,7 @@
         }
     }
 
-    if (stream->filetype == FILE_DTS) {
+    if (stream->filetype == FILE_DTS && (NULL == m8_lib_handle)) {
         m8_lib_handle = (qap_session_handle_t) qap_load_library(QAC_LIB_M8);
         if (m8_lib_handle == NULL) {
             fprintf(stdout, "Failed to load M8 library\n");
@@ -1176,7 +1178,7 @@
                 (stream->filetype == FILE_WAV) ||
                 (stream->filetype == FILE_AAC) ||
                 (stream->filetype == FILE_AAC_ADTS) ||
-                (stream->filetype == FILE_AAC_LATM)) {
+                (stream->filetype == FILE_AAC_LATM) && (NULL == ms12_lib_handle)) {
         ms12_lib_handle = (qap_session_handle_t) qap_load_library(QAC_LIB_MS12);
         if (ms12_lib_handle == NULL) {
             fprintf(stderr, "Failed to load MS12 library\n");
@@ -1307,6 +1309,16 @@
     session_output_configured = false;
     qap_session_close(qap_session_handle);
     qap_session_handle = NULL;
+    if (stream_cnt == 0) {
+        if (NULL != m8_lib_handle) {
+            qap_unload_library(m8_lib_handle);
+            m8_lib_handle = NULL;
+        }
+        if (NULL != ms12_lib_handle) {
+            qap_unload_library(ms12_lib_handle);
+            ms12_lib_handle = NULL;
+        }
+    }
 }
 
 void *qap_wrapper_start_stream (void* stream_data)
@@ -1572,7 +1584,8 @@
         return NULL;
     }
 
-    stream_close = false;
+    primary_stream_close = false;
+    stream_cnt ++;
     return qap_module_handle;
 
 }
@@ -1580,33 +1593,27 @@
 {
     char *token = NULL;
     char *strings[100] = {NULL};
-    char cmd_str[500] = {0};
+    char cmd_str[1024] = {0};
     char *tmp_str = NULL;
     int i = 0;
 
     do {
-        int j = 0, cnt = 1, status = 0;
+        int j = 0, cnt = 0, status = 0;
 
         if (fgets(cmd_str, sizeof(cmd_str), fp) != NULL)
             tmp_str = strdup(cmd_str);
         else
             break;
-        fprintf(stdout, "%s %d\n", __FUNCTION__, __LINE__);
+        fprintf(stdout, "%s %d tmp_str is %s", __FUNCTION__, __LINE__, tmp_str);
         token = strtok(tmp_str, " ");
         if (NULL != token) {
-            strings[cnt] = (char*)calloc(1, (strlen(token) +1));
-            memset(strings[cnt], '\0', strlen(token) +1);
-            strncpy(strings[cnt], token, strlen(token));
-            cnt++;
+            strings[cnt++] = strdup("playlist");
+            strings[cnt++] = strdup(token);
             while (NULL != (token = strtok(NULL, " "))) {
-                strings[cnt] = (char*)calloc(1, (strlen(token) +1));
-                memset(strings[cnt], '\0', strlen(token) +1);
-                strncpy(strings[cnt], token, strlen(token));
+                strings[cnt] = strdup(token);
+                ALOGV("%s %d strings[%d] is %s", __FUNCTION__, __LINE__, cnt, strings[cnt]);
                 cnt++;
             }
-            strings[0] = calloc(1, 4);
-            memset(strings[0], '\0', 4);
-            strncpy(strings[0], "play_list", 3);
             for (j = 0;j< cnt;j++) {
                 if (!strncmp(strings[j], "-f", 2)) {
                    (*stream_param)[i].filename = strdup(strings[j+1]);
@@ -1626,15 +1633,21 @@
                     (*stream_param)[i].aac_fmt_type = atoi(strings[j+1]);
                 }
             }
+            free(tmp_str);
+            tmp_str = NULL;
         }
         if(NULL != (*stream_param)[i].filename) {
             *num_of_streams = i+1;
             play_list = true;
-            kvp_str[i] = (char *)qap_wrapper_get_cmd_string_from_arg_array(cnt, (char**)strings, &status);
+            kvp_str[i] = (char *)qap_wrapper_get_cmd_string_from_arg_array(cnt, strings, &status);
+            ALOGV("%s %d kvp_str[%d] is %s", __FUNCTION__, __LINE__, i, kvp_str[i]);
         }
-        free(tmp_str);
-        for (j=0; j < cnt; j++)
-           free(strings[j]);
+        for (j=0; j < cnt; j++) {
+           if (NULL != strings[j]){
+               free(strings[j]);
+               strings[j] = NULL;
+           }
+        }
         i++;
     }while(NULL != cmd_str);