hal: Replace wall time in QC HAL code

Replace clock reference from REALTIME (wall clock) to MONOTONIC . This is to
avoid getting impacted when system time is set backwards or forwards (either
through network or manually)

Change-Id: I989c7bd0adc80ffb40f65ec909295ca1d8501877
diff --git a/hal/audio_extn/keep_alive.c b/hal/audio_extn/keep_alive.c
old mode 100755
new mode 100644
index 07b235d..ce2cbda
--- a/hal/audio_extn/keep_alive.c
+++ b/hal/audio_extn/keep_alive.c
@@ -119,6 +119,7 @@
     ka.userdata = adev;
     ka.state = STATE_IDLE;
     ka.pcm = NULL;
+    pthread_condattr_t attr;
     if (property_get_bool("vendor.audio.keep_alive.disabled", false)) {
         ALOGE("keep alive disabled");
         ka.state = STATE_DISABLED;
@@ -127,9 +128,12 @@
     ka.done = false;
     ka.prev_mode = KEEP_ALIVE_OUT_NONE;
     ka.active_devices = AUDIO_DEVICE_NONE;
+
     pthread_mutex_init(&ka.lock, (const pthread_mutexattr_t *) NULL);
     pthread_cond_init(&ka.cond, (const pthread_condattr_t *) NULL);
-    pthread_cond_init(&ka.wake_up_cond, (const pthread_condattr_t *) NULL);
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    pthread_cond_init(&ka.wake_up_cond, &attr);
     pthread_mutex_init(&ka.sleep_lock, (const pthread_mutexattr_t *) NULL);
     list_init(&ka.cmd_list);
     if (pthread_create(&ka.thread,  (const pthread_attr_t *) NULL,
@@ -434,9 +438,8 @@
              * Hence a short burst of silence periodically.
              */
             pthread_mutex_lock(&ka.sleep_lock);
-            clock_gettime(CLOCK_REALTIME, &ts);
+            clock_gettime(CLOCK_MONOTONIC, &ts);
             ts.tv_sec += SILENCE_INTERVAL;
-            ts.tv_nsec = 0;
 
             if (!ka.done)
               pthread_cond_timedwait(&ka.wake_up_cond,
diff --git a/hal/audio_extn/spkr_protection.c b/hal/audio_extn/spkr_protection.c
index e9d14b2..3e514cc 100644
--- a/hal/audio_extn/spkr_protection.c
+++ b/hal/audio_extn/spkr_protection.c
@@ -846,9 +846,8 @@
         goto exit;
     }
     cleanup = true;
-    clock_gettime(CLOCK_REALTIME, &ts);
+    clock_gettime(CLOCK_MONOTONIC, &ts);
     ts.tv_sec += (SLEEP_AFTER_CALIB_START/1000);
-    ts.tv_nsec = 0;
     pthread_mutex_lock(&handle.mutex_spkr_prot);
     pthread_mutex_unlock(&adev->lock);
     acquire_device = true;
@@ -963,9 +962,8 @@
 {
     struct timespec ts;
 
-    clock_gettime(CLOCK_REALTIME, &ts);
+    clock_gettime(CLOCK_MONOTONIC, &ts);
     ts.tv_sec += WAKEUP_MIN_IDLE_CHECK;
-    ts.tv_nsec = 0;
     pthread_mutex_lock(&handle.cal_wait_cond_mutex);
     pthread_cond_timedwait(&handle.cal_wait_condition,
                            &handle.cal_wait_cond_mutex, &ts);
@@ -1570,6 +1568,7 @@
 {
     char value[PROPERTY_VALUE_MAX];
     int result = 0;
+    pthread_condattr_t attr;
     ALOGD("%s: Initialize speaker protection module", __func__);
     memset(&handle, 0, sizeof(handle));
     if (!adev) {
@@ -1593,15 +1592,18 @@
     handle.trigger_cal = false;
     /* HAL for speaker protection is always calibrating for stereo usecase*/
     vi_feed_no_channels = spkr_vi_channels(adev);
-    pthread_cond_init(&handle.cal_wait_condition, NULL);
-    pthread_mutex_init(&handle.cal_wait_cond_mutex, NULL);
 
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    pthread_cond_init(&handle.cal_wait_condition, &attr);
+    pthread_mutex_init(&handle.cal_wait_cond_mutex, NULL);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
     if (is_wsa_present()) {
         if (platform_spkr_prot_is_wsa_analog_mode(adev) == 1) {
             ALOGD("%s: WSA analog mode", __func__);
             pcm_config_skr_prot.channels = WSA_ANALOG_MODE_CHANNELS;
         }
-        pthread_cond_init(&handle.spkr_calib_cancel, NULL);
+        pthread_cond_init(&handle.spkr_calib_cancel, &attr);
         pthread_cond_init(&handle.spkr_calibcancel_ack, NULL);
         pthread_mutex_init(&handle.mutex_spkr_prot, NULL);
         pthread_mutex_init(&handle.spkr_calib_cancelack_mutex, NULL);
@@ -1619,7 +1621,7 @@
         ALOGD("%s: WSA spkr calibration thread is not created", __func__);
     }
     pthread_cond_init(&handle.spkr_prot_thermalsync, NULL);
-    pthread_cond_init(&handle.spkr_calib_cancel, NULL);
+    pthread_cond_init(&handle.spkr_calib_cancel, &attr);
     pthread_cond_init(&handle.spkr_calibcancel_ack, NULL);
     pthread_mutex_init(&handle.mutex_spkr_prot, NULL);
     pthread_mutex_init(&handle.spkr_calib_cancelack_mutex, NULL);
diff --git a/mm-audio/aenc-aac/qdsp6/src/omx_aac_aenc.cpp b/mm-audio/aenc-aac/qdsp6/src/omx_aac_aenc.cpp
index 25b4bea..67596d2 100644
--- a/mm-audio/aenc-aac/qdsp6/src/omx_aac_aenc.cpp
+++ b/mm-audio/aenc-aac/qdsp6/src/omx_aac_aenc.cpp
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010-2017, The Linux Foundation. All rights reserved.
+Copyright (c) 2010-2018, 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:
@@ -158,9 +158,14 @@
     pthread_mutex_lock(&m_event_lock);
     while (0 == m_is_event_done)
     {
-       clock_gettime(CLOCK_REALTIME, &ts);
+       clock_gettime(CLOCK_MONOTONIC, &ts);
        ts.tv_sec += (SLEEP_MS/1000);
        ts.tv_nsec += ((SLEEP_MS%1000) * 1000000);
+       if (ts.tv_nsec >= 1000000000)
+       {
+          ts.tv_nsec -= 1000000000;
+          ts.tv_sec += 1;
+       }
        rc = pthread_cond_timedwait(&cond, &m_event_lock, &ts);
        if (rc == ETIMEDOUT && !m_is_event_done) {
          DEBUG_PRINT("Timed out waiting for flush");
@@ -305,6 +310,7 @@
 {
     int cond_ret = 0;
     component_Role.nSize = 0;
+    pthread_condattr_t attr;
     memset(&m_cmp, 0, sizeof(m_cmp));
     memset(&m_cb, 0, sizeof(m_cb));
     memset(&m_aac_pb_stats, 0, sizeof(m_aac_pb_stats));
@@ -350,7 +356,10 @@
 
     pthread_mutexattr_init(&in_buf_count_lock_attr);
     pthread_mutex_init(&in_buf_count_lock, &in_buf_count_lock_attr);
-    if ((cond_ret = pthread_cond_init (&cond, NULL)) != 0)
+
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    if ((cond_ret = pthread_cond_init (&cond, &attr)) != 0)
     {
        DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for cond\n");
        if (cond_ret == EAGAIN)
diff --git a/mm-audio/aenc-amrnb/qdsp6/src/omx_amr_aenc.cpp b/mm-audio/aenc-amrnb/qdsp6/src/omx_amr_aenc.cpp
index de0c063..4222b11 100644
--- a/mm-audio/aenc-amrnb/qdsp6/src/omx_amr_aenc.cpp
+++ b/mm-audio/aenc-amrnb/qdsp6/src/omx_amr_aenc.cpp
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010, 2014, 2016-2017, The Linux Foundation. All rights reserved.
+Copyright (c) 2010, 2014, 2016-2018, 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:
@@ -149,9 +149,14 @@
     pthread_mutex_lock(&m_event_lock);
     while (0 == m_is_event_done)
     {
-       clock_gettime(CLOCK_REALTIME, &ts);
+       clock_gettime(CLOCK_MONOTONIC, &ts);
        ts.tv_sec += (SLEEP_MS/1000);
        ts.tv_nsec += ((SLEEP_MS%1000) * 1000000);
+       if (ts.tv_nsec >= 1000000000)
+       {
+          ts.tv_nsec -= 1000000000;
+          ts.tv_sec += 1;
+       }
        rc = pthread_cond_timedwait(&cond, &m_event_lock, &ts);
        if (rc == ETIMEDOUT && !m_is_event_done) {
          DEBUG_PRINT("Timed out waiting for flush");
@@ -295,6 +300,7 @@
 {
     int cond_ret = 0;
     component_Role.nSize = 0;
+    pthread_condattr_t attr;
     memset(&m_cmp, 0, sizeof(m_cmp));
     memset(&m_cb, 0, sizeof(m_cb));
     memset(&m_pcm_param, 0, sizeof(m_pcm_param));
@@ -340,7 +346,10 @@
 
     pthread_mutexattr_init(&in_buf_count_lock_attr);
     pthread_mutex_init(&in_buf_count_lock, &in_buf_count_lock_attr);
-    if ((cond_ret = pthread_cond_init (&cond, NULL)) != 0)
+
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    if ((cond_ret = pthread_cond_init (&cond, &attr)) != 0)
     {
        DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for cond\n");
        if (cond_ret == EAGAIN)
diff --git a/mm-audio/aenc-evrc/qdsp6/src/omx_evrc_aenc.cpp b/mm-audio/aenc-evrc/qdsp6/src/omx_evrc_aenc.cpp
index 45020c1..d854ed2 100644
--- a/mm-audio/aenc-evrc/qdsp6/src/omx_evrc_aenc.cpp
+++ b/mm-audio/aenc-evrc/qdsp6/src/omx_evrc_aenc.cpp
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010, 2014-2017, The Linux Foundation. All rights reserved.
+Copyright (c) 2010, 2014-2018, 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:
@@ -149,9 +149,14 @@
     pthread_mutex_lock(&m_event_lock);
     while (0 == m_is_event_done)
     {
-       clock_gettime(CLOCK_REALTIME, &ts);
+       clock_gettime(CLOCK_MONOTONIC, &ts);
        ts.tv_sec += (SLEEP_MS/1000);
        ts.tv_nsec += ((SLEEP_MS%1000) * 1000000);
+       if (ts.tv_nsec >= 1000000000)
+       {
+          ts.tv_nsec -= 1000000000;
+          ts.tv_sec += 1;
+       }
        rc = pthread_cond_timedwait(&cond, &m_event_lock, &ts);
        if (rc == ETIMEDOUT && !m_is_event_done) {
          DEBUG_PRINT("Timed out waiting for flush");
@@ -292,6 +297,7 @@
         m_ipc_to_cmd_th(NULL)
 {
     int cond_ret = 0;
+    pthread_condattr_t attr;
     memset(&m_cmp, 0, sizeof(m_cmp));
     memset(&m_cb, 0, sizeof(m_cb));
     memset(&m_evrc_param, 0, sizeof(m_evrc_param));
@@ -337,7 +343,10 @@
 
     pthread_mutexattr_init(&in_buf_count_lock_attr);
     pthread_mutex_init(&in_buf_count_lock, &in_buf_count_lock_attr);
-    if ((cond_ret = pthread_cond_init (&cond, NULL)) != 0)
+
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    if ((cond_ret = pthread_cond_init (&cond, &attr)) != 0)
     {
        DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for cond\n");
        if (cond_ret == EAGAIN)
diff --git a/mm-audio/aenc-g711/qdsp6/src/omx_g711_aenc.cpp b/mm-audio/aenc-g711/qdsp6/src/omx_g711_aenc.cpp
index 6f94e58..5aa4989 100644
--- a/mm-audio/aenc-g711/qdsp6/src/omx_g711_aenc.cpp
+++ b/mm-audio/aenc-g711/qdsp6/src/omx_g711_aenc.cpp
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010, 2014, 2016-2017 The Linux Foundation. All rights reserved.
+Copyright (c) 2010, 2014, 2016-2018 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
@@ -151,9 +151,14 @@
     pthread_mutex_lock(&m_event_lock);
     while (0 == m_is_event_done)
     {
-       clock_gettime(CLOCK_REALTIME, &ts);
+       clock_gettime(CLOCK_MONOTONIC, &ts);
        ts.tv_sec += (SLEEP_MS/1000);
        ts.tv_nsec += ((SLEEP_MS%1000) * 1000000);
+       if (ts.tv_nsec >= 1000000000)
+       {
+          ts.tv_nsec -= 1000000000;
+          ts.tv_sec += 1;
+       }
        rc = pthread_cond_timedwait(&cond, &m_event_lock, &ts);
        if (rc == ETIMEDOUT && !m_is_event_done) {
             OMX_LOGV("Timed out waiting for flush");
@@ -297,6 +302,7 @@
 {
     int cond_ret = 0;
     component_Role.nSize = 0;
+    pthread_condattr_t attr;
     memset(&m_cmp, 0, sizeof(m_cmp));
     memset(&m_cb, 0, sizeof(m_cb));
     memset(&m_g711_pb_stats, 0, sizeof(m_g711_pb_stats));
@@ -344,7 +350,9 @@
     pthread_mutex_init(&in_buf_count_lock, &in_buf_count_lock_attr);
     updateLogLevel();
 
-    if ((cond_ret = pthread_cond_init (&cond, NULL)) != 0)
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    if ((cond_ret = pthread_cond_init (&cond, &attr)) != 0)
     {
        OMX_LOGE("pthread_cond_init returns non zero for cond\n");
        if (cond_ret == EAGAIN)
diff --git a/mm-audio/aenc-qcelp13/qdsp6/src/omx_qcelp13_aenc.cpp b/mm-audio/aenc-qcelp13/qdsp6/src/omx_qcelp13_aenc.cpp
index 6cdf8e6..7b76e14 100644
--- a/mm-audio/aenc-qcelp13/qdsp6/src/omx_qcelp13_aenc.cpp
+++ b/mm-audio/aenc-qcelp13/qdsp6/src/omx_qcelp13_aenc.cpp
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010, 2014-2017, The Linux Foundation. All rights reserved.
+Copyright (c) 2010, 2014-2018, 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:
@@ -149,9 +149,14 @@
     pthread_mutex_lock(&m_event_lock);
     while (0 == m_is_event_done)
     {
-       clock_gettime(CLOCK_REALTIME, &ts);
+       clock_gettime(CLOCK_MONOTONIC, &ts);
        ts.tv_sec += (SLEEP_MS/1000);
        ts.tv_nsec += ((SLEEP_MS%1000) * 1000000);
+       if (ts.tv_nsec >= 1000000000)
+       {
+          ts.tv_nsec -= 1000000000;
+          ts.tv_sec += 1;
+       }
        rc = pthread_cond_timedwait(&cond, &m_event_lock, &ts);
        if (rc == ETIMEDOUT && !m_is_event_done) {
           DEBUG_PRINT("Timed out waiting for flush");
@@ -296,6 +301,7 @@
 {
     int cond_ret = 0;
     component_Role.nSize = 0;
+    pthread_condattr_t attr;
     memset(&m_cmp, 0, sizeof(m_cmp));
     memset(&m_cb, 0, sizeof(m_cb));
     memset(&m_qcelp13_pb_stats, 0, sizeof(m_qcelp13_pb_stats));
@@ -341,7 +347,10 @@
 
     pthread_mutexattr_init(&in_buf_count_lock_attr);
     pthread_mutex_init(&in_buf_count_lock, &in_buf_count_lock_attr);
-    if ((cond_ret = pthread_cond_init (&cond, NULL)) != 0)
+
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    if ((cond_ret = pthread_cond_init (&cond, &attr)) != 0)
     {
        DEBUG_PRINT_ERROR("pthread_cond_init returns non zero for cond\n");
        if (cond_ret == EAGAIN)
diff --git a/qahw_api/test/qahw_multi_record_test.c b/qahw_api/test/qahw_multi_record_test.c
index 2e0a396..7be6512 100644
--- a/qahw_api/test/qahw_multi_record_test.c
+++ b/qahw_api/test/qahw_multi_record_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2015 The Android Open Source Project *
@@ -404,7 +404,7 @@
       }
 
       if (kpi_mode && count == 0) {
-          ret = clock_gettime(CLOCK_REALTIME, &tsColdI);
+          ret = clock_gettime(CLOCK_MONOTONIC, &tsColdI);
           if (ret)
               fprintf(log_file, "error(%d) getting current time before first read!, handle(%d)", ret, params->handle);
       }
@@ -415,14 +415,14 @@
 
       if (kpi_mode) {
           if (count == 0) {
-              ret = clock_gettime(CLOCK_REALTIME, &tsColdF);
+              ret = clock_gettime(CLOCK_MONOTONIC, &tsColdF);
               if (ret)
                   fprintf(log_file, "error(%d) getting current time after first read!, handle(%d)", ret, params->handle);
           } else if (count == 8) {
           /* 8th read done time is captured in kernel which would have trigger 9th read in DSP
            * 9th read is received by usersace at this time
            */
-              ret = clock_gettime(CLOCK_REALTIME, &tsCont);
+              ret = clock_gettime(CLOCK_MONOTONIC, &tsCont);
               if (ret)
                   fprintf(log_file, "error(%d) getting current time after 8th read!, handle(%d)", ret, params->handle);
           }
diff --git a/qahw_api/test/qahw_playback_test.c b/qahw_api/test/qahw_playback_test.c
index ba7f4bf..4160754 100644
--- a/qahw_api/test/qahw_playback_test.c
+++ b/qahw_api/test/qahw_playback_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2015 The Android Open Source Project *
@@ -1166,7 +1166,7 @@
             bytes_remaining = write_length = bytes_wanted;
         }
         if (count == 0) {
-            ret = clock_gettime(CLOCK_REALTIME, &ts_cold);
+            ret = clock_gettime(CLOCK_MONOTONIC, &ts_cold);
             if (ret) {
                 fprintf(log_file, "error(%d) fetching start time for cold latency", ret);
                 fprintf(stderr, "error(%d) fetching start time for cold latency", ret);
@@ -1176,7 +1176,7 @@
         } else if (count == 16) {
             int *d = (int *)data;
             d[0] = 0x01010000;
-            ret = clock_gettime(CLOCK_REALTIME, &ts_cont);
+            ret = clock_gettime(CLOCK_MONOTONIC, &ts_cont);
             if (ret) {
                 fprintf(log_file, "error(%d) fetching start time for continuous latency", ret);
                 fprintf(stderr, "error(%d) fetching start time for continuous latency", ret);