hal: Fix backend configuration during stream switch

-Currently, input stream sample rate and/or bit width is compared
with current backend sample rate and/or bit width. Backend is
re-configured only if input stream's rate/width is higher. This
causes incorrect sampling rate during stream switch of varying
sample rates.
-The backend is configured at highest bit width and/or sample rate
amongst all active playback usecases.
-By setting the backend re-configuration flag only when the selected
sample rate and/or bit width differ with current backend sample rate
and/or bit width, re-routing is enabled and backend is re-configured.
-Exception: 16 bit playbacks is allowed through 16/48 backend only.
-Enable logs for app type, acdb id and sample rate.

Change-Id: I09bfe30ebf8bdb3f9b481d7375ce7450fa67270c
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index 0f35f87..aa22193 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -518,7 +518,8 @@
     app_type_cfg[len++] = out->app_type_cfg.sample_rate;
 
     mixer_ctl_set_array(ctl, app_type_cfg, len);
-
+    ALOGI("%s app_type %d, acdb_dev_id %d, sample_rate %d",
+           __func__, out->app_type_cfg.app_type, acdb_dev_id, out->app_type_cfg.sample_rate);
     rc = 0;
 exit_send_app_type_cfg:
     return rc;
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index c01ba77..9f5da7d 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2541,6 +2541,8 @@
         out->sample_rate = out->config.rate;
     }
 
+    ALOGV("%s flags %x, format %x, out->sample_rate %d, out->bit_width %d",
+           __func__, flags, format, out->sample_rate, out->bit_width);
     audio_extn_utils_update_stream_app_type_cfg(adev->platform,
                                                 &adev->streams_output_cfg_list,
                                                 flags, format, out->sample_rate,
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 1e6f681..3622f72 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -2442,6 +2442,8 @@
     bool backend_change = false;
     struct listnode *node;
     struct stream_out *out = NULL;
+    unsigned int bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
+    unsigned int sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
 
     // For voice calls use default configuration
     // force routing is not required here, caller will do it anyway
@@ -2453,10 +2455,16 @@
         backend_change = true;
     }
 
-
+    /*
+     * The backend should be configured at highest bit width and/or
+     * sample rate amongst all playback usecases.
+     * If the selected sample rate and/or bit width differ with
+     * current backend sample rate and/or bit width, then, we set the
+     * backend re-configuration flag.
+     *
+     * Exception: 16 bit playbacks is allowed through 16 bit/48 khz backend only
+     */
     if (!backend_change) {
-        // Go through all the playback usecases
-        // Find the max bit width and samplerate
         list_for_each(node, &adev->usecase_list) {
             struct audio_usecase *curr_usecase;
             curr_usecase = node_to_item(node, struct audio_usecase, list);
@@ -2466,21 +2474,27 @@
                 if (out != NULL ) {
                     ALOGV("Offload playback running bw %d sr %d",
                               out->bit_width, out->sample_rate);
-                        if (*new_bit_width < out->bit_width)
-                            *new_bit_width = out->bit_width;
-                        if (*new_sample_rate < out->sample_rate)
-                            *new_sample_rate = out->sample_rate;
+                        if (bit_width < out->bit_width)
+                            bit_width = out->bit_width;
+                        if (sample_rate < out->sample_rate)
+                            sample_rate = out->sample_rate;
                 }
             }
         }
     }
 
+    // 16 bit playbacks is allowed through 16 bit/48 khz backend only
+    if (16 == bit_width)
+        sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
     // Force routing if the expected bitwdith or samplerate
     // is not same as current backend comfiguration
-    if ((*new_bit_width != adev->cur_codec_backend_bit_width) ||
-        (*new_sample_rate != adev->cur_codec_backend_samplerate)) {
+    if ((bit_width != adev->cur_codec_backend_bit_width) ||
+        (sample_rate != adev->cur_codec_backend_samplerate)) {
+        *new_bit_width = bit_width;
+        *new_sample_rate = sample_rate;
         backend_change = true;
-        ALOGW("Codec backend needs to be updated");
+        ALOGI("%s Codec backend needs to be updated. new bit width: %d new sample rate: %d",
+               __func__, *new_bit_width, *new_sample_rate);
     }
 
     return backend_change;
@@ -2506,12 +2520,6 @@
     if (platform_check_codec_backend_cfg(adev, usecase,
                                       &new_bit_width, &new_sample_rate)) {
         platform_set_codec_backend_cfg(adev, new_bit_width, new_sample_rate);
-    }
-
-    if (old_bit_width != adev->cur_codec_backend_bit_width ||
-        old_sample_rate != adev->cur_codec_backend_samplerate) {
-        ALOGW("New codec backend bit width %d, sample rate %d",
-                    adev->cur_codec_backend_bit_width, adev->cur_codec_backend_samplerate);
         return true;
     }