am b052eae4: Fixes a race condition in the hardware mp3 OMX driver code.

Merge commit 'b052eae44afe8a1febceb2287d728af9e0df4828' into froyo-plus-aosp

* commit 'b052eae44afe8a1febceb2287d728af9e0df4828':
  Fixes a race condition in the hardware mp3 OMX driver code.
diff --git a/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Dec_Utils.c b/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Dec_Utils.c
index 2909d9a..91709f6 100644
--- a/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Dec_Utils.c
+++ b/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Dec_Utils.c
@@ -551,6 +551,25 @@
     return eError;
 }
 
+static void signalAlloBufThresholdIfNecessary(
+        MP3DEC_COMPONENT_PRIVATE *pComponentPrivate) {
+#ifndef UNDER_CE
+    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
+    if (pComponentPrivate->AlloBuf_waitingsignal) {
+        pComponentPrivate->AlloBuf_waitingsignal = 0;
+        pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
+    }
+    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
+#else
+    if (pComponentPrivate->AlloBuf_waitingsignal) {
+        // I am fairly sure this will suffer from similar issues without
+        // proper mutex protection and a loop under WinCE...
+        pComponentPrivate->AlloBuf_waitingsignal = 0;
+        OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
+    }
+#endif
+}
+
 
 /* ================================================================================= * */
 /**
@@ -1469,17 +1488,8 @@
                 pComponentPrivate->pPortDef[MP3D_OUTPUT_PORT]->bEnabled = OMX_TRUE;
                 OMX_PRCOMM2(pComponentPrivate->dbg, "pComponentPrivate->pPortDef[MP3D_OUTPUT_PORT]->bEnabled = %d\n",
                               pComponentPrivate->pPortDef[MP3D_OUTPUT_PORT]->bEnabled);
-                
-                if(pComponentPrivate->AlloBuf_waitingsignal){
-                    pComponentPrivate->AlloBuf_waitingsignal = 0;
-#ifndef UNDER_CE             
-                    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex); 
-                    pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
-                    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);    
-#else
-                    OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
-#endif            
-                }
+
+                signalAlloBufThresholdIfNecessary(pComponentPrivate);
                 OMX_PRCOMM2(pComponentPrivate->dbg, "setting output port to enabled\n");
             }
         }
@@ -1493,13 +1503,8 @@
                                                        MP3D_INPUT_PORT,
                                                        NULL);
                 
-                if(pComponentPrivate->AlloBuf_waitingsignal){
-                    pComponentPrivate->AlloBuf_waitingsignal = 0;           
-                    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex); 
-                    pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
-                    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);    
-                }
-                
+                signalAlloBufThresholdIfNecessary(pComponentPrivate);
+
                 //MP3DECFill_LCMLInitParamsEx(pHandle, 0);
                 // queue the pending buffers received while doing the config
                 for (i=0; i < pComponentPrivate->nNumInputBufPending; i++) {
@@ -1540,12 +1545,7 @@
                                                         MP3D_OUTPUT_PORT, 
                                                         NULL);
 
-                if(pComponentPrivate->AlloBuf_waitingsignal){
-                    pComponentPrivate->AlloBuf_waitingsignal = 0;           
-                    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex); 
-                    pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
-                    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);    
-                }
+                signalAlloBufThresholdIfNecessary(pComponentPrivate);
                 OMX_PRINT2(pComponentPrivate->dbg, "reconfigOut = %d!, but should be true!\n",pComponentPrivate->reconfigOutputPort);
                 if(pComponentPrivate->reconfigOutputPort){
                     //make sure new VA's are used
@@ -1625,12 +1625,7 @@
                                                        MP3D_OUTPUT_PORT, 
                                                        NULL);
 
-                if(pComponentPrivate->AlloBuf_waitingsignal){
-                    pComponentPrivate->AlloBuf_waitingsignal = 0;           
-                    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex); 
-                    pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
-                    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);    
-                }
+                signalAlloBufThresholdIfNecessary(pComponentPrivate);
                 MP3DEC_CleanupInitParamsEx(pHandle,commandData);
                 MP3DECFill_LCMLInitParamsEx(pHandle, -1);
 
diff --git a/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Decoder.c b/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Decoder.c
index 7703961..5eb55c7 100644
--- a/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Decoder.c
+++ b/omx/audio/src/openmax_il/mp3_dec/src/OMX_Mp3Decoder.c
@@ -1821,7 +1821,24 @@
     return eError;
 }
 
-
+static void waitForAlloBufThreshold(
+        MP3DEC_COMPONENT_PRIVATE *pComponentPrivate) {
+#ifndef UNDER_CE
+    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
+    pComponentPrivate->AlloBuf_waitingsignal = 1;
+    while (pComponentPrivate->AlloBuf_waitingsignal) {
+        pthread_cond_wait(
+                &pComponentPrivate->AlloBuf_threshold,
+                &pComponentPrivate->AlloBuf_mutex);
+    }
+    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
+#else
+    // I am fairly sure this will suffer from similar issues without
+    // proper mutex protection and a loop under WinCE...
+    pComponentPrivate->AlloBuf_waitingsignal = 1;
+    OMX_WaitForEvent(&(pComponentPrivate->AlloBuf_event));
+#endif
+}
 
 /* ================================================================================= * */
 /**
@@ -1875,14 +1892,7 @@
     MP3D_OMX_CONF_CHECK_CMD(pPortDef, 1, 1);
 
     if(!pPortDef->bEnabled){
-        pComponentPrivate->AlloBuf_waitingsignal = 1;  
-#ifndef UNDER_CE        
-        pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex); 
-        pthread_cond_wait(&pComponentPrivate->AlloBuf_threshold, &pComponentPrivate->AlloBuf_mutex);
-        pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
-#else
-        OMX_WaitForEvent(&(pComponentPrivate->AlloBuf_event));
-#endif
+        waitForAlloBufThreshold(pComponentPrivate);
     }
 
     OMX_MALLOC_GENERIC(pBufferHeader, OMX_BUFFERHEADERTYPE);
@@ -2273,11 +2283,8 @@
                 pComponentPrivate)->pPortDef[nPortIndex];
 
     MP3D_OMX_CONF_CHECK_CMD(pPortDef, 1, 1);
-    if(!pPortDef->bEnabled){
-        pComponentPrivate->AlloBuf_waitingsignal = 1;   
-        pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex); 
-        pthread_cond_wait(&pComponentPrivate->AlloBuf_threshold, &pComponentPrivate->AlloBuf_mutex);
-        pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
+    if(!pPortDef->bEnabled) {
+        waitForAlloBufThreshold(pComponentPrivate);
     }
     OMX_PRCOMM2(pComponentPrivate->dbg, ":: pPortDef = %p\n",pPortDef);
     OMX_PRCOMM2(pComponentPrivate->dbg, ":: pPortDef->bEnabled = %d\n",pPortDef->bEnabled);