audiohal: Modifications in hal_play_test for trumpet certification

- Enumerated list of trumpet parameters added
- Dolby's proprietary codes have been moved to the
  test library for certification purposes

Change-Id: I8a8d13c05ba0617cdef945498f988e27ce704486
(cherry picked from commit b6b5bd4e75fd61d0bc729e7827cfd4695f50b3df)
diff --git a/qahw_api/Makefile.am b/qahw_api/Makefile.am
index 38506b8..e939111 100644
--- a/qahw_api/Makefile.am
+++ b/qahw_api/Makefile.am
@@ -5,7 +5,8 @@
             inc/qahw_effect_equalizer.h \
             inc/qahw_effect_presetreverb.h \
             inc/qahw_effect_virtualizer.h \
-            inc/qahw_effect_visualizer.h
+            inc/qahw_effect_visualizer.h \
+            inc/qahw_effect_trumpet.h
 
 AM_CPPFLAGS := -I $(top_srcdir)/qahw_api/inc/
 AM_CPPFLAGS += -I $(PKG_CONFIG_SYSROOT_DIR)/usr/include/mm-audio/audio-hal-server
diff --git a/qahw_api/inc/qahw_effect_trumpet.h b/qahw_api/inc/qahw_effect_trumpet.h
new file mode 100644
index 0000000..3257f7b
--- /dev/null
+++ b/qahw_api/inc/qahw_effect_trumpet.h
@@ -0,0 +1,114 @@
+/* Copyright (c) 2019, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef QAHW_EFFECT_TRUMPET_H_
+#define QAHW_EFFECT_TRUMPET_H_
+
+#include <qahw_effect_api.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define QAHW_EFFECT_TRUMPET_LIBRARY "libtrumpet.so"
+
+static const qahw_effect_uuid_t SL_IID_TRUMPET_ = {0x5e46f50b, 0xcc86, 0x4d6e, 0x9610, {0x54, 0x72, 0x76, 0x1d, 0xd0, 0xe2}};
+static const qahw_effect_uuid_t * const SL_IID_TRUMPET = &SL_IID_TRUMPET_;
+
+static const qahw_effect_uuid_t SL_IID_TRUMPET_UUID_ = {0xa52a402b, 0xe254, 0x4fb9, 0xab7a, {0x7c, 0xf5, 0x83, 0x44, 0xe3, 0xf3}};
+static const qahw_effect_uuid_t * const SL_IID_TRUMPET_UUID = &SL_IID_TRUMPET_UUID_;
+
+/* enumerated parameter settings for trumpet effect used by both set and get*/
+typedef enum {
+    TRUMPET_PARAM_ENABLE,
+    TRUMPET_PARAM_PREGAIN,
+    TRUMPET_PARAM_POSTGAIN,
+    TRUMPET_PARAM_SYSTEMGAIN,
+    TRUMPET_PARAM_MI_DV_LEVELER_STEERING,
+    TRUMPET_PARAM_MI_DIALOG_ENHANCER,
+    TRUMPET_PARAM_MI_SURROUND_COMPRESSOR,
+    TRUMPET_PARAM_MI_IEQ_STEERING,
+    TRUMPET_PARAM_DIALOG_AMOUNT,
+    TRUMPET_PARAM_DIALOG_DUCKING,
+    TRUMPET_PARAM_DIALOG_ENABLE,
+    TRUMPET_PARAM_VOLUME_LEVELER_AMOUNT,
+    TRUMPET_PARAM_VOLUME_LEVELER_IN_TARGET,
+    TRUMPET_PARAM_VOLUME_LEVELER_OUT_TARGET,
+    TRUMPET_PARAM_VOLUME_LEVELER_ENABLE,
+    TRUMPET_PARAM_VOLUME_MODELER_CALIBRATION,
+    TRUMPET_PARAM_VOLUME_MODELER_ENABLE,
+    TRUMPET_PARAM_VOLMAX_BOOST,
+    TRUMPET_PARAM_BASS_BOOST,
+    TRUMPET_PARAM_BASS_CUTOFF_FREQ,
+    TRUMPET_PARAM_BASS_WIDTH,
+    TRUMPET_PARAM_BASS_ENABLE,
+    TRUMPET_PARAM_BASS_EXTRACT_CUTOFF_FREQ,
+    TRUMPET_PARAM_BASS_EXTRACT_ENABLE,
+    TRUMPET_PARAM_REGULATOR_SET,
+    TRUMPET_PARAM_REGULATOR_OVERDRIVE,
+    TRUMPET_PARAM_REGULATOR_TIMBRE_PRESERVE,
+    TRUMPET_PARAM_REGULATOR_RELAXATION_AMT,
+    TRUMPET_PARAM_REGULATOR_SPKR_DIST_ENABLE,
+    TRUMPET_PARAM_REGULATOR_ENABLE,
+    TRUMPET_PARAM_VIRTUAL_BASS_MODE,
+    TRUMPET_PARAM_VIRTUAL_BASS_SRC_FREQ,
+    TRUMPET_PARAM_VIRTUAL_BASS_MIX_FREQ,
+    TRUMPET_PARAM_VIRTUAL_BASS_OVERALL_GAIN,
+    TRUMPET_PARAM_VIRTUAL_BASS_SUBGAINS,
+    TRUMPET_PARAM_VIRTUAL_BASS_SLOPE_GAIN,
+    TRUMPET_PARAM_FRONT_SPK_ANG,
+    TRUMPET_PARAM_SURROUND_SPK_ANG,
+    TRUMPET_PARAM_HEIGHT_SPK_ANG,
+    TRUMPET_PARAM_HEIGHT_FILTER_MODE,
+    TRUMPET_PARAM_SURROUND_BOOST,
+    TRUMPET_PARAM_SURROUND_DECODER_ENABLE,
+    TRUMPET_PARAM_CALIBRATION,
+    TRUMPET_PARAM_GRAPHICS_ENABLE,
+    TRUMPET_PARAM_GRAPHICS_SET,
+    TRUMPET_PARAM_AUDIO_OPTIMIZER_ENABLE,
+    TRUMPET_PARAM_AUDIO_OPTIMIZER_SET,
+    TRUMPET_PARAM_PROCESS_OPTIMIZER_ENABLE,
+    TRUMPET_PARAM_PROCESS_OPTIMIZER_SET,
+    TRUMPET_PARAM_IEQ_ENABLE,
+    TRUMPET_PARAM_IEQ_AMOUNT,
+    TRUMPET_PARAM_IEQ_SET,
+    TRUMPET_PARAM_OP_MODE,
+    TRUMPET_PARAM_DYNAMIC_PARAMETER,
+    TRUMPET_PARAM_INIT_INFO,
+    TRUMPET_PARAM_CENTER_SPREADING,
+    TRUMPET_PARAM_METADATA_PARAM,
+    TRUMPET_PARAM_CIDK_VALIDATE = 100,
+} qahw_trumpet_params_t;
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+
+#endif /*QAHW_EFFECT_TRUMPET_H_*/
diff --git a/qahw_api/test/Makefile.am b/qahw_api/test/Makefile.am
index 40cc24a..ddf1be2 100644
--- a/qahw_api/test/Makefile.am
+++ b/qahw_api/test/Makefile.am
@@ -2,9 +2,10 @@
 
 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
 
+PLAY_INCLUDES = -I $(top_srcdir)/qahw_api/inc
+PLAY_INCLUDES += -I $(top_srcdir)/qahw/inc \
+                -I $(top_srcdir)/hal
 hal_play_test_SOURCES = qahw_playback_test.c \
                         qahw_effect_test.c
 
@@ -21,6 +22,23 @@
 hal_play_test_CFLAGS = $(PLAY_CFLAGS) $(PLAY_INCLUDES)
 hal_play_test_CFLAGS += $(AM_CFLAGS) -DLINUX_ENABLED
 
+#hal_play_test_dolby_certification
+bin_PROGRAMS += hal_play_test_dolby_certification
+
+hal_play_test_dolby_certification_SOURCES = qahw_playback_test.c \
+                                            qahw_effect_test.c
+
+hal_play_test_dolby_certification_LDADD = -lutils ../libqahw.la
+
+if QAP
+hal_play_test_dolby_certification_SOURCES += qap_wrapper_extn.c
+hal_play_test_dolby_certification_LDADD += -lqap_wrapper
+endif
+
+hal_play_test_dolby_certification_CFLAGS = $(PLAY_CFLAGS) $(PLAY_INCLUDES) -DTRUMPET_CERTIFICATION=1
+hal_play_test_dolby_certification_CFLAGS += $(AM_CFLAGS) -DLINUX_ENABLED
+
+#hal_play_rec
 bin_PROGRAMS += hal_rec_test
 
 REC_CFLAGS = $(PLAY_CFLAGS) -Dnullptr=NULL
diff --git a/qahw_api/test/qahw_playback_test.c b/qahw_api/test/qahw_playback_test.c
index a048d3e..a5e10ed 100644
--- a/qahw_api/test/qahw_playback_test.c
+++ b/qahw_api/test/qahw_playback_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2015 The Android Open Source Project *
@@ -36,9 +36,9 @@
 #define KV_PAIR_MAX_LENGTH  1000
 
 #define FORMAT_PCM 1
-#define WAV_HEADER_LENGTH_MAX 128
+#define WAV_HEADER_LENGTH_MAX  256
 
-#define MAX_PLAYBACK_STREAMS   105 //This value is changed to suppport 100 clips in playlist
+#define MAX_PLAYBACK_STREAMS   105 //This value is changed to suppport 100 clips in the playlist
 #define PRIMARY_STREAM_INDEX   0
 
 #define KVPAIRS_MAX 100
@@ -47,6 +47,7 @@
 #define FORMAT_DESCRIPTOR_SIZE 12
 #define SUBCHUNK1_SIZE(x) ((8) + (x))
 #define SUBCHUNK2_SIZE 8
+#define DATA_CHUNK_SIZE 8
 
 #define DEFAULT_PRESET_STRENGTH -1
 
@@ -62,6 +63,12 @@
 static void init_streams(void);
 int pthread_cancel(pthread_t thread);
 
+//START OF TRUMPET TEST
+#ifdef TRUMPET_CERTIFICATION
+#include <dlfcn.h>
+#endif
+//END OF TRUMPET TEST
+
 struct wav_header {
     uint32_t riff_id;
     uint32_t riff_sz;
@@ -501,9 +508,26 @@
         return true;
     return false;
 }
-static int read_bytes(stream_config *stream, void *buff, int size) {
-    if (stream->filename)
-        return fread(buff, 1, size, stream->file_stream);
+static int read_bytes(stream_config *stream, void *buff, int size, unsigned int *total_bytes_read) {
+    unsigned int bytes_read = 0;
+    if (stream->filename) {
+
+        if (stream->filetype == FILE_WAV) {
+
+            /* Read only valid data from a wav file and ignore incase of any metadata at the end*/
+            if (size < stream->raw_data_len_in_bytes - *total_bytes_read) {
+                bytes_read = fread(buff, 1, size, stream->file_stream);
+                *total_bytes_read += bytes_read;
+                return (int)bytes_read;
+            } else {
+                bytes_read = fread(buff, 1, stream->raw_data_len_in_bytes - *total_bytes_read, stream->file_stream);
+                *total_bytes_read += bytes_read;
+                return (int)bytes_read;
+            }
+        } else {
+            return fread(buff, 1, size, stream->file_stream);
+        }
+    }
     else if (AUDIO_DEVICE_NONE != stream->input_device) {
         qahw_in_buffer_t in_buf;
         memset(&in_buf,0, sizeof(qahw_in_buffer_t));
@@ -675,6 +699,23 @@
         qahw_out_set_callback(params->out_handle, async_callback, params);
     }
 
+    /* Mapping for 10 channels*/
+    struct qahw_out_channel_map_param chmap_param ={0};
+    chmap_param.channels = params->channels;
+    if (chmap_param.channels == 10) {
+        chmap_param.channel_map[0] = QAHW_PCM_CHANNEL_FL ;
+        chmap_param.channel_map[1] = QAHW_PCM_CHANNEL_FR ;
+        chmap_param.channel_map[2] = QAHW_PCM_CHANNEL_FC ;
+        chmap_param.channel_map[3] = QAHW_PCM_CHANNEL_LFE ;
+        chmap_param.channel_map[4] = QAHW_PCM_CHANNEL_LB ;
+        chmap_param.channel_map[5] = QAHW_PCM_CHANNEL_RB;
+        chmap_param.channel_map[6] = 18 ;
+        chmap_param.channel_map[7] = 19 ;
+        chmap_param.channel_map[8] = QAHW_PCM_CHANNEL_FLC ;
+        chmap_param.channel_map[9] = QAHW_PCM_CHANNEL_FRC ;
+        qahw_out_set_param_data(params->out_handle, QAHW_PARAM_OUT_CHANNEL_MAP, (qahw_param_payload *) &chmap_param);
+    }
+
     // create effect thread, use thread_data to transfer command
     if (params->ethread_func &&
             (params->flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD|AUDIO_OUTPUT_FLAG_DIRECT))) {
@@ -805,11 +846,11 @@
             fprintf(stderr, "stream %s: failed to set kvpairs\n", params->set_params);
         }
     }
-
+    unsigned int total_bytes_read = 0;
     while (!exit && !stop_playback) {
         if (!bytes_remaining) {
             fprintf(log_file, "\nstream %d: reading bytes %zd\n", params->stream_index, bytes_wanted);
-            bytes_read = read_bytes(params, data_ptr, bytes_wanted);
+            bytes_read = read_bytes(params, data_ptr, bytes_wanted, &total_bytes_read);
             fprintf(log_file, "stream %d: read bytes %zd\n", params->stream_index, bytes_read);
             if ((!read_complete_file && (bytes_to_read <= 0)) || (bytes_read <= 0)) {
                 fprintf(log_file, "stream %d: end of file\n", params->stream_index);
@@ -1032,7 +1073,7 @@
 
     char header[WAV_HEADER_LENGTH_MAX] = {0};
     int wav_header_len = 0;
-
+    int file_length = 0;
     switch (stream_info->filetype) {
         case FILE_WAV:
             /*
@@ -1042,6 +1083,13 @@
                 fprintf(log_file, "wav header length is invalid:%d\n", wav_header_len);
                 exit(1);
             }
+            if(wav_header_len > WAV_HEADER_LENGTH_MAX)
+            {
+                fprintf(log_file, " Wave header length is not supported to exiting\n");
+                exit(1);
+            }
+            fseek(stream_info->file_stream, 0, SEEK_END);
+            file_length = ftell(stream_info->file_stream);
             fseek(stream_info->file_stream, 0, SEEK_SET);
             rc = fread (header, wav_header_len , 1, stream_info->file_stream);
             if (rc != 1) {
@@ -1057,6 +1105,13 @@
             memcpy (&stream_info->channels, &header[22], 2);
             memcpy (&stream_info->config.offload_info.sample_rate, &header[24], 4);
             memcpy (&stream_info->config.offload_info.bit_width, &header[34], 2);
+            /*Check if it's a data chunk*/
+            if (0 == strncmp (&header[wav_header_len - 8], "data", 4)) {
+                memcpy (&stream_info->raw_data_len_in_bytes, &header[wav_header_len - 4],4);
+                fprintf(log_file, "wav data length is:%d bytes\n", stream_info->raw_data_len_in_bytes);
+            } else {
+                stream_info->raw_data_len_in_bytes = file_length - wav_header_len;
+            }
             if (stream_info->config.offload_info.bit_width == 32)
                 stream_info->config.offload_info.format = AUDIO_FORMAT_PCM_32_BIT;
             else if (stream_info->config.offload_info.bit_width == 24)
@@ -1643,19 +1698,40 @@
 int get_wav_header_length (FILE* file_stream)
 {
     int subchunk_size = 0, wav_header_len = 0;
-
+    char chunk_id[5];
+    chunk_id[4] = '\0';
     fseek(file_stream, 16, SEEK_SET);
+    wav_header_len += FORMAT_DESCRIPTOR_SIZE; //FORMAT_DESCRIPTOR_SIZE
+    wav_header_len += 4; // 'fmt '
+
     if(fread(&subchunk_size, 4, 1, file_stream) != 1) {
         fprintf(log_file, "Unable to read subchunk:\n");
         fprintf(stderr, "Unable to read subchunk:\n");
         exit (1);
     }
+    wav_header_len += 4; // chunk_1_size
+
     if(subchunk_size < 16) {
         fprintf(log_file, "This is not a valid wav file \n");
         fprintf(stderr, "This is not a valid wav file \n");
     } else {
-         wav_header_len = FORMAT_DESCRIPTOR_SIZE + SUBCHUNK1_SIZE(subchunk_size) + SUBCHUNK2_SIZE;
+        fseek(file_stream, subchunk_size, SEEK_CUR); /* Read complet 'fmt 'chunck size*/
+        wav_header_len += subchunk_size; // chunk_1_size
+        while(fread(chunk_id, 4, 1, file_stream)) {
+            wav_header_len += 4; /* 4 bytes chunk id*/
+
+            if(!strncmp (chunk_id, "data", 4)) {
+                wav_header_len += 4;
+                break;
+            } else {
+                fread(&subchunk_size, 4, 1, file_stream);
+                wav_header_len +=  4;
+                fseek(file_stream, subchunk_size, SEEK_CUR);
+                wav_header_len += subchunk_size;
+            }
+        }
     }
+    printf("Wave Header Length: %d\n", wav_header_len);
     return wav_header_len;
 }
 
@@ -2584,6 +2660,33 @@
             fprintf(log_file, "stream %d: play_later = %d\n", i, stream_param[i].play_later);
         }
 
+//START OF TRUMPET TEST
+#ifdef TRUMPET_CERTIFICATION
+
+      void *handle_trumpet = dlopen("/usr/lib/libtrumpetcertification.so", RTLD_NOW | RTLD_GLOBAL);
+      const char* error_trumpet = NULL;
+
+      if (handle_trumpet == NULL) {
+          fprintf(stderr, "failed to open libtrumpetcertification.so\n");
+      } else {
+          typedef int32_t (*_trumpet_test_main)(int, char **);  //prototype of the certification function
+          _trumpet_test_main trumpet_test_main = (_trumpet_test_main) dlsym(handle_trumpet, "trumpet_test_main");
+          error_trumpet = dlerror();
+
+          if (error_trumpet != NULL)
+              fprintf(stderr, "%s\n", error_trumpet);
+
+          if (trumpet_test_main == NULL) {
+              perror("dlsym");
+              fprintf(stderr, "Failed to find the function trumpet_test_main in libtrumpetcertification.so\n");
+          } else {
+              trumpet_test_main(argc, argv);
+          }
+          dlclose(handle_trumpet);
+      }
+#endif
+//END OF TRUMPET TEST
+
         rc = pthread_create(&playback_thread[i], NULL, start_stream_playback, (void *)&stream_param[i]);
         if (rc) {
             fprintf(log_file, "stream %d: failed to create thread\n", stream->stream_index);
diff --git a/qahw_api/test/qahw_playback_test.h b/qahw_api/test/qahw_playback_test.h
index b8bddbc..6ca6a67 100644
--- a/qahw_api/test/qahw_playback_test.h
+++ b/qahw_api/test/qahw_playback_test.h
@@ -144,6 +144,7 @@
     pthread_mutex_t input_buffer_available_lock;
     uint32_t input_buffer_available_size;
     char *set_params;
+    unsigned int raw_data_len_in_bytes;
 }stream_config;
 
 qahw_module_handle_t * load_hal(audio_devices_t dev);