increase the bluetooth task priority when start a2dp.

change the BT task priority based on audio play state.
increase the BT task priority to ANDROID_PRIORITY_URGENT_AUDIO
,when start ad2p audio playing.
to better prevent CPU premption by other process/task(UI).
restore the BT task priority when stop a2dp audio playing.

bug:12082841
Change-Id: I34e8344cffea87f68987149c820cd3e84a4196d1
diff --git a/btif/src/btif_av.c b/btif/src/btif_av.c
index f173f6a..46a32d1 100755
--- a/btif/src/btif_av.c
+++ b/btif/src/btif_av.c
@@ -39,6 +39,7 @@
 #include "gki.h"
 #include "bd.h"
 #include "btu.h"
+#include "bt_utils.h"
 
 /*****************************************************************************
 **  Constants & Macros
@@ -586,9 +587,17 @@
 
             HAL_CBACK(bt_av_callbacks, audio_state_cb,
                 BTAV_AUDIO_STATE_STARTED, &(btif_av_cb.peer_bda));
+
+            /* increase the a2dp consumer task priority temporarily when start
+            ** audio playing, to avoid overflow the audio packet queue. */
+            adjust_priority_a2dp(TRUE);
+
             break;
 
         case BTIF_SM_EXIT_EVT:
+            /* restore the a2dp consumer task priority when stop audio playing. */
+            adjust_priority_a2dp(FALSE);
+
             break;
 
         case BTIF_AV_START_STREAM_REQ_EVT:
diff --git a/utils/include/bt_utils.h b/utils/include/bt_utils.h
index ac18f07..722f92e 100644
--- a/utils/include/bt_utils.h
+++ b/utils/include/bt_utils.h
@@ -39,5 +39,6 @@
 void bt_utils_init();
 void bt_utils_cleanup();
 void raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task);
+void adjust_priority_a2dp(int start);
 
 #endif /* BT_UTILS_H */
diff --git a/utils/src/bt_utils.c b/utils/src/bt_utils.c
index aeb9292..6decacf 100644
--- a/utils/src/bt_utils.c
+++ b/utils/src/bt_utils.c
@@ -50,6 +50,8 @@
 static BOOLEAN g_DoSchedulingGroup[TASK_HIGH_MAX];
 static pthread_mutex_t         gIdxLock;
 static int g_TaskIdx;
+static int g_TaskIDs[TASK_HIGH_MAX];
+#define INVALID_TASK_ID  (-1)
 
 /*****************************************************************************
 **
@@ -67,6 +69,7 @@
     for(i = 0; i < TASK_HIGH_MAX; i++) {
         g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
         g_DoSchedulingGroup[i] = TRUE;
+        g_TaskIDs[i] = INVALID_TASK_ID;
     }
     pthread_mutexattr_init(&lock_attr);
     pthread_mutex_init(&gIdxLock, &lock_attr);
@@ -126,6 +129,7 @@
         // set_sched_policy does not support tid == 0
         rc = set_sched_policy(tid, SP_FOREGROUND);
     }
+    g_TaskIDs[high_task] = tid;
     pthread_mutex_unlock(&gIdxLock);
 
     if (rc) {
@@ -137,3 +141,31 @@
     }
 }
 
+/*****************************************************************************
+**
+** Function        adjust_priority_a2dp
+**
+** Description     increase the a2dp consumer task priority temporarily when start
+**                 audio playing, to avoid overflow the audio packet queue, restore
+**                 the a2dp consumer task priority when stop audio playing.
+**
+** Returns         void
+**
+*******************************************************************************/
+void adjust_priority_a2dp(int start) {
+    int priority = start ? ANDROID_PRIORITY_URGENT_AUDIO : ANDROID_PRIORITY_AUDIO;
+    int tid;
+    int i;
+
+    for (i = TASK_HIGH_GKI_TIMER; i < TASK_HIGH_MAX; i++)
+    {
+        tid = g_TaskIDs[i];
+        if (tid != INVALID_TASK_ID)
+        {
+            if (setpriority(PRIO_PROCESS, tid, priority) < 0)
+            {
+                ALOGW("failed to change priority tid: %d to %d", tid, priority);
+            }
+        }
+    }
+}