hal: MS12: qap test app update

- updating qap hal test app to test new features of
MS12-1.3.2
- Added Support for Main2 Testing
- Added Support for OTT sound Testing
- Added Support to send init time params to MS12

Change-Id: I7713509b1bac11c04e7af8f3447624d1727b35f3
diff --git a/qahw_api/test/qap_wrapper_extn.c b/qahw_api/test/qap_wrapper_extn.c
index 9cd9fd6..d311570 100644
--- a/qahw_api/test/qap_wrapper_extn.c
+++ b/qahw_api/test/qap_wrapper_extn.c
@@ -109,7 +109,10 @@
 pthread_mutex_t main_eos_lock;
 pthread_cond_t sec_eos_cond;
 pthread_mutex_t sec_eos_lock;
+pthread_cond_t main2_eos_cond;
+pthread_mutex_t main2_eos_lock;
 bool main_eos_received = false;
+bool main2_eos_received = false;
 bool sec_eos_received = false;
 
 dlb_ms12_session_param_t dlb_param;
@@ -796,7 +799,7 @@
     if(p_stream_param == NULL) {
         ALOGE("%s %d, callback handle is null.",__func__,__LINE__);
     }
-    ALOGV("%s %d Received event id %d\n", __func__, __LINE__, event_id);
+    ALOGV("%s %d, %s Received event id %d\n", __func__, __LINE__, p_stream_param->filename, event_id);
 
     switch (event_id) {
         case QAP_MODULE_CALLBACK_EVENT_SEND_INPUT_BUFFER:
@@ -856,7 +859,6 @@
             main_eos_received = true;
             pthread_mutex_unlock(&main_eos_lock);
 
-            ALOGE("%s %d Received Main Input EOS ", __func__, __LINE__);
             if (!stream_cnt)
                 close_output_streams();
             if (play_list_cnt && input_streams_count) {
@@ -865,10 +867,10 @@
             }
             break;
         case QAP_CALLBACK_EVENT_EOS_ASSOC:
-        case QAP_CALLBACK_EVENT_MAIN_2_EOS:
             if (stream_cnt > 0)
                 stream_cnt--;
-            if (!has_system_input){
+            //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);
@@ -878,6 +880,20 @@
             if (!stream_cnt)
                 close_output_streams();
             break;
+        case QAP_CALLBACK_EVENT_MAIN_2_EOS:
+            if (stream_cnt > 0)
+                stream_cnt--;
+            //if (!has_system_input)
+            {
+                ALOGV("%s %d Received main2 Input EOS", __func__, __LINE__);
+                pthread_mutex_lock(&main2_eos_lock);
+                pthread_cond_signal(&main2_eos_cond);
+                main2_eos_received = true;
+                pthread_mutex_unlock(&main2_eos_lock);
+            }
+            if (!stream_cnt)
+                close_output_streams();
+            break;
         case QAP_CALLBACK_EVENT_ERROR:
             break;
         case QAP_CALLBACK_EVENT_SUCCESS:
@@ -1284,6 +1300,95 @@
     }
 }
 
+void update_qap_session_init_params(char *kv_pairs)
+{
+    int status = 0;
+    char *kvp = NULL;
+    int temp = 0;
+    int *temp_val = NULL;
+    uint32_t cmd_data[16] = {0};
+    uint32_t cmd_size = 0;
+
+    kvp = qap_wrapper_get_single_kvp("max_chs", kv_pairs, &status);
+    if (kvp != NULL) {
+        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
+        if (temp_val != NULL) {
+            cmd_data[cmd_size++] = MS12_SESSION_CFG_MAX_CHS;
+            cmd_data[cmd_size++] = temp_val[0];
+            free(temp_val);
+            temp_val = NULL;
+        }
+        free(kvp);
+        kvp = NULL;
+    }
+
+    kvp = qap_wrapper_get_single_kvp("bs_out_mode", kv_pairs, &status);
+    if (kvp != NULL) {
+        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
+        if (temp_val != NULL) {
+            cmd_data[cmd_size++] = MS12_SESSION_CFG_BS_OUTPUT_MODE;
+            cmd_data[cmd_size++] = temp_val[0];
+            free(temp_val);
+            temp_val = NULL;
+        }
+        free(kvp);
+        kvp = NULL;
+    }
+
+    kvp = qap_wrapper_get_single_kvp("chmod_locking", kv_pairs, &status);
+    if (kvp != NULL) {
+        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
+        if (temp_val != NULL) {
+            cmd_data[cmd_size++] = MS12_SESSION_CFG_CHMOD_LOCKING;
+            cmd_data[cmd_size++] = temp_val[0];
+            free(temp_val);
+            temp_val = NULL;
+        }
+        free(kvp);
+        kvp = NULL;
+    }
+
+    kvp = qap_wrapper_get_single_kvp("dn", kv_pairs, &status);
+    if (kvp != NULL) {
+        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
+        if (temp_val != NULL) {
+            cmd_data[cmd_size++] = MS12_SESSION_CFG_DIALOG_NORM;
+            cmd_data[cmd_size++] = temp_val[0];
+            free(temp_val);
+            temp_val = NULL;
+        }
+        free(kvp);
+        kvp = NULL;
+    }
+
+    kvp = qap_wrapper_get_single_kvp("rp", kv_pairs, &status);
+    if (kvp != NULL) {
+        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
+        if (temp_val != NULL) {
+            cmd_data[cmd_size++] = MS12_SESSION_CFG_COMPR_PROF;
+            cmd_data[cmd_size++] = temp_val[0];
+            free(temp_val);
+            temp_val = NULL;
+        }
+        free(kvp);
+        kvp = NULL;
+    }
+
+    if (!cmd_size) {
+        return;
+    }
+
+    temp = qap_session_cmd(qap_session_handle,
+            QAP_SESSION_CMD_SET_PARAM,
+            cmd_size * sizeof(uint32_t),
+            &cmd_data[0],
+            NULL,
+            NULL);
+    if (temp != QAP_STATUS_OK) {
+        fprintf(stderr, "session init config failed\n");
+    }
+}
+
 int qap_wrapper_session_open(char *kv_pairs, void* stream_data, int num_of_streams,  qahw_module_handle_t *hal_handle)
 {
     int status = 0;
@@ -1380,7 +1485,7 @@
 
     if ((session_type == SESSION_BROADCAST) && dolby_formats) {
         fprintf(stdout, "%s::%d Setting BROADCAST session for dolby formats\n", __func__, __LINE__);
-        qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_BROADCAST, ms12_lib_handle);
+        qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_MS12_OTT, ms12_lib_handle);
         if (qap_session_handle == NULL)
             return -EINVAL;
     } else if ((session_type == SESSION_BROADCAST) && !dolby_formats) {
@@ -1419,6 +1524,10 @@
         return -EINVAL;
     }
 
+    if (dolby_formats) {
+        update_qap_session_init_params(kv_pairs);
+    }
+
     if (!session_output_configured) {
         if (session_type != SESSION_BROADCAST)
             out_sample_rate = stream->config.sample_rate;;
@@ -1438,6 +1547,7 @@
 
         bitwidth_kvp = qap_wrapper_get_single_kvp("k", kv_pairs, &status);
         if (bitwidth_kvp && strncmp(bitwidth_kvp, "k=", 2) == 0) {
+            fprintf(stdout, "Session set params, kvpair %s\n",&bitwidth_kvp[2]);
             ret = qap_session_cmd(qap_session_handle, QAP_SESSION_CMD_SET_KVPAIRS, (sizeof(bitwidth_kvp) - 2), &bitwidth_kvp[2], NULL, NULL);
             if (ret != QAP_STATUS_OK)
                 fprintf(stderr, "Session set params failed\n");
@@ -1447,8 +1557,10 @@
     }
 
     pthread_mutex_init(&main_eos_lock, (const pthread_mutexattr_t *)NULL);
+    pthread_mutex_init(&main2_eos_lock, (const pthread_mutexattr_t *)NULL);
     pthread_mutex_init(&sec_eos_lock, (const pthread_mutexattr_t *)NULL);
     pthread_cond_init(&main_eos_cond, (const pthread_condattr_t *) NULL);
+    pthread_cond_init(&main2_eos_cond, (const pthread_condattr_t *) NULL);
     pthread_cond_init(&sec_eos_cond, (const pthread_condattr_t *) NULL);
     fprintf(stdout, "Session open returing success\n");
     return 0;
@@ -1559,11 +1671,11 @@
         }
 
         bytes_wanted = buffer->common_params.size;
-        bytes_read = fread(data_buf, sizeof(unsigned char), bytes_wanted, fp_input);
+        bytes_read = fread(buffer->common_params.data, sizeof(unsigned char), bytes_wanted, fp_input);
 
         buffer->common_params.offset = 0;
         buffer->common_params.size = bytes_read;
-        memcpy(buffer->common_params.data, data_buf, bytes_read);
+        //memcpy(buffer->common_params.data, data_buf, bytes_read);
         if (bytes_read <= 0 || stop_playback) {
             buffer->buffer_parms.input_buf_params.flags = QAP_BUFFER_EOS;
             bytes_consumed = qap_module_process(qap_module_handle, buffer);
@@ -1597,26 +1709,10 @@
                 buffer->common_params.data += bytes_consumed;
                 buffer->common_params.size -= bytes_consumed;
             }
-            ALOGV("%s %d feeding Input of size %d  and bytes_cosumed is %d",
-                      __FUNCTION__, __LINE__,bytes_read, bytes_consumed);
-            if (stream_info->filetype == FILE_DTS) {
+            ALOGV("%s %d, %s feeding Input of size %d  and bytes_cosumed is %d",
+                      __FUNCTION__, __LINE__,stream_info->filename, bytes_read, bytes_consumed);
+            {
                 if (bytes_consumed < 0) {
-#if 0
-                    while (!is_buffer_available) {
-                        usleep(1000);
-                        ret = qap_module_cmd(qap_module_handle, QAP_MODULE_CMD_GET_PARAM,
-                                             sizeof(QAP_MODULE_CMD_GET_PARAM), "buf_available", NULL, reply_data
-                        );
-                        if (reply_data)
-                            temp_str = get_string_value(reply_data, &status);
-                        if (temp_str) {
-                            is_buffer_available = atoi(temp_str);
-                            free(temp_str);
-                        }
-                        ALOGV("%s : %d, dts clip reply_data is %d buffer availabale is %d",
-                              __FUNCTION__, __LINE__, reply_data, is_buffer_available);
-                    }
-#else
                     pthread_mutex_lock(&stream_info->input_buffer_available_lock);
                     stream_info->input_buffer_available_size = 0;
                     pthread_mutex_unlock(&stream_info->input_buffer_available_lock);
@@ -1633,7 +1729,6 @@
                                      stream_info->filename,
                                      stream_info->input_buffer_available_size);
                     }
-#endif
                     if(kpi_mode && time_index > 5) {
                         gettimeofday(&tcont_ts1, NULL);
                         data_input_st_arr[time_index] = (tcont_ts1.tv_sec) * 1000 + (tcont_ts1.tv_usec) / 1000;
@@ -1651,14 +1746,25 @@
 
 wait_for_eos:
     if (stream_info->sec_input) {
-        if (!sec_eos_received) {
-            pthread_mutex_lock(&sec_eos_lock);
-            pthread_cond_wait(&sec_eos_cond, &sec_eos_lock);
-            pthread_mutex_unlock(&sec_eos_lock);
+        if (!(stream_info->flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
+            if (!main2_eos_received) {
+                pthread_mutex_lock(&main2_eos_lock);
+                pthread_cond_wait(&main2_eos_cond, &main2_eos_lock);
+                pthread_mutex_unlock(&main2_eos_lock);
+            }
+            main2_eos_received = false;
+            fprintf(stdout, "Received EOS event for main2 input\n");
+            ALOGV("Received EOS event for main2 input\n");
+        } else {
+            if (!sec_eos_received) {
+                pthread_mutex_lock(&sec_eos_lock);
+                pthread_cond_wait(&sec_eos_cond, &sec_eos_lock);
+                pthread_mutex_unlock(&sec_eos_lock);
+            }
+            sec_eos_received = false;
+            fprintf(stdout, "Received EOS event for secondary input\n");
+            ALOGV("Received EOS event for secondary input\n");
         }
-        sec_eos_received = false;
-        fprintf(stdout, "Received EOS event for secondary input\n");
-        ALOGV("Received EOS event for secondary input\n");
     }
     if (!(stream_info->system_input || stream_info->sec_input)){
         if (!main_eos_received) {
@@ -1711,29 +1817,38 @@
     else
         stream_info->bytes_to_read = 1024;
     input_streams_count++;
-    if (input_streams_count == 2) {
-        if (stream_info->filetype == FILE_WAV) {
-            input_config.flags = QAP_MODULE_FLAG_SYSTEM_SOUND;
-            stream_info->system_input = true;
-            has_system_input = true;
-            ALOGV("%s::%d Set Secondary System Sound Flag", __func__, __LINE__);
-        } else if (stream_info->filetype != FILE_WAV) {
+
+    if (stream_info->filetype == FILE_WAV) {
+        switch (stream_info->flags)
+        {
+            case QAP_MODULE_FLAG_SYSTEM_SOUND:
+                ALOGV("%s::%d Set System Sound Flag", __func__, __LINE__);
+                break;
+            case QAP_MODULE_FLAG_APP_SOUND:
+                ALOGV("%s::%d Set System APP Flag", __func__, __LINE__);
+                break;
+            case QAP_MODULE_FLAG_OTT_SOUND:
+                ALOGV("%s::%d Set OTT Sound Flag", __func__, __LINE__);
+                break;
+            default:
+                ALOGE("%s::%d unsupported flag for PCM input.", __func__, __LINE__);
+                return NULL;
+        }
+        input_config.flags = stream_info->flags;
+        stream_info->system_input = true;
+        has_system_input = true;
+    } else {
+        if (input_streams_count > 1) {
             if (stream_info->flags & AUDIO_OUTPUT_FLAG_ASSOCIATED) {
-                 ALOGV("%s::%d Set Secondary Assoc Input Flag", __func__, __LINE__);
-                 input_config.flags = QAP_MODULE_FLAG_SECONDARY;
-                 stream_info->sec_input = true;
+                ALOGV("%s::%d Set Secondary Assoc Input Flag", __func__, __LINE__);
+                input_config.flags = QAP_MODULE_FLAG_SECONDARY;
+                stream_info->sec_input = true;
             } else {
                 ALOGV("%s::%d Set Secondary Main Input Flag", __func__, __LINE__);
                 input_config.flags = QAP_MODULE_FLAG_PRIMARY;
                 stream_info->sec_input = true;
             }
-        }
-        stream_info->bytes_to_read = 2048;
-    } else {
-        if (stream_info->filetype == FILE_WAV) {
-            ALOGV("%s::%d Set Secondary System Sound Flag", __func__, __LINE__);
-            input_config.flags = QAP_MODULE_FLAG_SYSTEM_SOUND;
-            stream_info->system_input = true;
+            stream_info->bytes_to_read = 2048;
         } else {
             if (stream_info->flags & AUDIO_OUTPUT_FLAG_ASSOCIATED) {
                 ALOGV("%s::%d Set Secondary Assoc Input Flag", __func__, __LINE__);