Merge 905e30c35f427cde04d24de70949c94e80897780 on remote branch

Change-Id: I3136cbfa6e3cdd76971eaf4d2075db19a7831c6a
diff --git a/SoundTriggerDevice.cpp b/SoundTriggerDevice.cpp
index 4a7d388..cd9d616 100644
--- a/SoundTriggerDevice.cpp
+++ b/SoundTriggerDevice.cpp
@@ -470,7 +470,6 @@
     /* Register LSM Lib HIDL service */
     ALOGD("%s: Register LSM HIDL service", __func__);
     sp<IListenSoundModel> service = new ListenSoundModel();
-    configureRpcThreadpool(48, false /*callerWillJoin*/);
     if(android::OK !=  service->registerAsService())
         ALOGW("Could not register LSM HIDL service");
 #endif
@@ -531,6 +530,7 @@
     int status = 0;
     char audio_hal_lib[100];
     void *apiVersion = nullptr;
+    audio_extn_hidl_init initAudioExtn = nullptr;
 
     ALOGD("%s: Enter", __func__);
 
@@ -579,6 +579,14 @@
         ALOGD("%s: ahal is using API version 0x%04x", __func__,
               sthal_prop_api_version_);
     }
+
+    initAudioExtn = (audio_extn_hidl_init)dlsym(ahal_handle_, "check_init_audio_extension");
+    if (!initAudioExtn) {
+        ALOGW("%s: error, failed to get symbol for initAudioExtn",
+              __func__);
+    } else {
+        initAudioExtn();
+    }
     return status;
 
 error:
diff --git a/SoundTriggerPropIntf.h b/SoundTriggerPropIntf.h
index aedff6b..a2cb7a0 100644
--- a/SoundTriggerPropIntf.h
+++ b/SoundTriggerPropIntf.h
@@ -169,4 +169,7 @@
 
 /* AHAL extn util function which is called by STHAL */
 typedef void (*audio_hw_close_snd_mixer_t)(struct mixer *mixer);
+
+/* AHAL extn util function which is called by STHAL */
+typedef void (*audio_extn_hidl_init)();
 #endif /* SOUND_TRIGGER_PROP_INTF_H */
diff --git a/SoundTriggerSession.cpp b/SoundTriggerSession.cpp
index 2dfa978..62a15f6 100644
--- a/SoundTriggerSession.cpp
+++ b/SoundTriggerSession.cpp
@@ -81,7 +81,7 @@
 SoundTriggerSession::SoundTriggerSession(sound_model_handle_t handle,
                                          audio_hw_call_back_t callback)
 {
-    state_ = IDLE;
+    UpdateState(IDLE);
     sm_handle_ = handle;
     rec_config_payload_ = nullptr;
     rec_config_ = nullptr;
@@ -91,8 +91,9 @@
 
 SoundTriggerSession::~SoundTriggerSession()
 {
-    if (pal_handle_ && state_ != IDLE)
+    if (pal_handle_ && IsState(IDLE))
         pal_stream_close(pal_handle_);
+
     pal_handle_ = nullptr;
 
     if (rec_config_payload_) {
@@ -140,9 +141,9 @@
      */
     do {
         lock_status = session->ses_mutex_.try_lock();
-    } while(!lock_status && (session->state_ == ACTIVE));
+    } while(!lock_status && session->IsState(ACTIVE));
 
-    if (session->state_ != ACTIVE) {
+    if (!session->IsState(ACTIVE)) {
         ALOGW("%s: skip notification as client has stopped", __func__);
         goto exit;
     }
@@ -228,13 +229,10 @@
 
     // callback to SoundTriggerService
     session->GetRecognitionCallback(&callback);
-    session->ses_mutex_.unlock();
-    lock_status = false;
     ATRACE_BEGIN("sthal: client detection callback");
-    if (session->state_ == ACTIVE)
-        callback(st_event, session->GetCookie());
-    else
-        ALOGW("%s: skip detection callback as client has stopped", __func__);
+
+    callback(st_event, session->GetCookie());
+
     ATRACE_END();
 
 exit:
@@ -318,6 +316,7 @@
     int status = 0;
 
     ALOGV("%s: Enter", __func__);
+    UpdateState(STOPPING);
 
     // deregister from audio hal
     RegisterHalEvent(false);
@@ -335,7 +334,7 @@
     }
     rec_config_ = nullptr;
 
-    state_ = STOPPED;
+    UpdateState(STOPPED);
     ALOGV("%s: Exit, status = %d", __func__, status);
 
     return status;
@@ -485,7 +484,7 @@
         goto exit;
     }
 
-    state_ = LOADED;
+    UpdateState(LOADED);
 
 exit:
     if (param_payload)
@@ -501,7 +500,7 @@
 
     ALOGV("%s: Enter", __func__);
     std::lock_guard<std::mutex> lck(ses_mutex_);
-    if (state_ == ACTIVE) {
+    if (IsState(ACTIVE)) {
         status = StopRecognition_l();
         if (status) {
             ALOGE("%s: error, failed to stop recognition, status = %d",
@@ -521,7 +520,7 @@
     }
     rec_config_ = nullptr;
 
-    state_ = IDLE;
+    UpdateState(IDLE);
 
     ALOGV("%s: Exit, status = %d", __func__, status);
 
@@ -609,7 +608,7 @@
               __func__, status);
         goto exit;
     }
-    state_ = ACTIVE;
+    UpdateState(ACTIVE);
 
     // register to audio hal
     RegisterHalEvent(true);
@@ -633,23 +632,8 @@
     ALOGV("%s: Enter", __func__);
     std::lock_guard<std::mutex> lck(ses_mutex_);
 
-    // deregister from audio hal
-    RegisterHalEvent(false);
+    StopRecognition_l();
 
-    // stop pal stream
-    status = pal_stream_stop(pal_handle_);
-    if (status) {
-        ALOGE("%s: error, failed to stop pal stream, status = %d",
-              __func__, status);
-    }
-
-    if (rec_config_payload_) {
-        free(rec_config_payload_);
-        rec_config_payload_ = nullptr;
-    }
-    rec_config_ = nullptr;
-
-    state_ = STOPPED;
     ALOGV("%s: Exit, status = %d", __func__, status);
 
     return status;
@@ -739,4 +723,3 @@
 {
     *callback = rec_callback_;
 }
-
diff --git a/SoundTriggerSession.h b/SoundTriggerSession.h
index 29236b7..79dfc86 100644
--- a/SoundTriggerSession.h
+++ b/SoundTriggerSession.h
@@ -26,6 +26,37 @@
  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+/* Changes from Qualcomm Innovation Center are provided under the following license:
+ *
+ * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted (subject to the limitations in the
+ * disclaimer below) 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 Qualcomm Innovation Center, Inc. nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
+ * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 SOUND_TRIGGER_SESSION_H
 #define SOUND_TRIGGER_SESSION_H
@@ -71,6 +102,8 @@
  protected:
     int OpenPALStream(pal_stream_type_t stream_type);
     bool IsACDSoundModel(struct sound_trigger_sound_model *sound_model);
+    bool IsState(session_state_t state) { return state_ == state; } //Call this with session mutex
+    void UpdateState(session_state_t state) { state_ = state; } //Call this with session mutex
     void RegisterHalEvent(bool is_register);
     static int pal_callback(pal_stream_handle_t *stream_handle,
         uint32_t event_id, uint32_t *event_data,
@@ -88,4 +121,4 @@
     std::mutex ses_mutex_;
 };
 
-#endif  // SOUND_TRIGGER_SESSION_H
\ No newline at end of file
+#endif  // SOUND_TRIGGER_SESSION_H