Merge pie-platform-release to aosp-master - DO NOT MERGE

Change-Id: I0c325ba2909c3bb14772a9d47e9076d0f2a4f5e2
diff --git a/src/android/AudioPlayer_to_android.cpp b/src/android/AudioPlayer_to_android.cpp
index e361dcb..de27cae 100644
--- a/src/android/AudioPlayer_to_android.cpp
+++ b/src/android/AudioPlayer_to_android.cpp
@@ -117,9 +117,9 @@
          case ANDROID_UNINITIALIZED:
              *pObjState = ANDROID_PREPARING;
              ap->prepare();
-             // intended fall through
+             FALLTHROUGH_INTENDED;
          case ANDROID_PREPARING:
-             // intended fall through
+             FALLTHROUGH_INTENDED;
          case ANDROID_READY:
              ap->play();
              break;
@@ -1064,6 +1064,7 @@
             // checkDataFormat() already checked representation
             df_representation = &df_pcm->representation;
             } // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
+            FALLTHROUGH_INTENDED;
         case SL_DATAFORMAT_PCM: {
             // checkDataFormat() already did generic checks, now do the Android-specific checks
             const SLDataFormat_PCM *df_pcm = (const SLDataFormat_PCM *) pAudioSrc->pFormat;
@@ -1360,6 +1361,7 @@
     case android::AudioTrack::EVENT_LOOP_END:
     case android::AudioTrack::EVENT_STREAM_END:
         // These are unexpected so fall through
+        FALLTHROUGH_INTENDED;
     default:
         // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit?
         SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event,
@@ -1598,7 +1600,7 @@
             break;
         }
         pAudioPlayer->mPerformanceMode = ANDROID_PERFORMANCE_MODE_LATENCY_EFFECTS;
-        /* FALL THROUGH */
+        FALLTHROUGH_INTENDED;
     case ANDROID_PERFORMANCE_MODE_LATENCY_EFFECTS:
         if ((flags & AUDIO_OUTPUT_FLAG_FAST) == 0) {
             pAudioPlayer->mPerformanceMode = ANDROID_PERFORMANCE_MODE_NONE;
@@ -1960,16 +1962,22 @@
     SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer);
     switch (pAudioPlayer->mAndroidObjType) {
 
-    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-throughk, both types of players
-                                           //   use the TrackPlayerBase for playback
     case AUDIOPLAYER_FROM_URIFD:
+        if (pAudioPlayer->mObject.mEngine->mAudioManager != 0) {
+            pAudioPlayer->mObject.mEngine->mAudioManager->releasePlayer(pAudioPlayer->mPIId);
+        }
+        // intended fall-throughk, both types of players
+        // use the TrackPlayerBase for playback
+        FALLTHROUGH_INTENDED;
+    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
         if (pAudioPlayer->mTrackPlayer != 0) {
             pAudioPlayer->mTrackPlayer->destroy();
         }
-
-        // intended fall-through
-    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
-    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through
+        FALLTHROUGH_INTENDED;
+    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
+    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
     case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
         pAudioPlayer->mAPlayer.clear();
         break;
@@ -2262,8 +2270,10 @@
         }
         break;
 
-    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:     // intended fall-through
-    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:  // intended fall-through
+    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
+    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
     case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
         // FIXME report and use the return code to the lock mechanism, which is where play state
         //   changes are updated (see object_unlock_exclusive_attributes())
@@ -2337,7 +2347,8 @@
     CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
     switch (ap->mAndroidObjType) {
 
-      case AUDIOPLAYER_FROM_URIFD:  // intended fall-through
+      case AUDIOPLAYER_FROM_URIFD:
+        FALLTHROUGH_INTENDED;
       case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
         int32_t durationMsec = ANDROID_UNKNOWN_TIME;
         if (ap->mAPlayer != 0) {
@@ -2522,6 +2533,7 @@
       } break;
     case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
       // nothing to do here, fall through
+      FALLTHROUGH_INTENDED;
     default:
       break;
     }
diff --git a/src/android/AudioRecorder_to_android.cpp b/src/android/AudioRecorder_to_android.cpp
index 38e50bf..4e2f31d 100644
--- a/src/android/AudioRecorder_to_android.cpp
+++ b/src/android/AudioRecorder_to_android.cpp
@@ -245,6 +245,7 @@
             // checkDataFormat() already checked representation
             df_representation = &df_pcm->representation;
         } // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
+            FALLTHROUGH_INTENDED;
         case SL_DATAFORMAT_PCM: {
             const SLDataFormat_PCM *df_pcm = (const SLDataFormat_PCM *) pAudioSnk->pFormat;
             // checkDataFormat already checked sample rate, channels, and mask
@@ -618,7 +619,7 @@
             break;
         }
         ar->mPerformanceMode = ANDROID_PERFORMANCE_MODE_LATENCY_EFFECTS;
-        /* FALL THROUGH */
+        FALLTHROUGH_INTENDED;
     case ANDROID_PERFORMANCE_MODE_LATENCY_EFFECTS:
         if ((flags & AUDIO_INPUT_FLAG_FAST) == 0) {
             ar->mPerformanceMode = ANDROID_PERFORMANCE_MODE_NONE;
diff --git a/src/android/MediaPlayer_to_android.cpp b/src/android/MediaPlayer_to_android.cpp
index 7d8d1af..5b150f4 100644
--- a/src/android/MediaPlayer_to_android.cpp
+++ b/src/android/MediaPlayer_to_android.cpp
@@ -334,7 +334,8 @@
         }
     } break;
 
-    case XA_DATALOCATOR_URI: // intended fall-through
+    case XA_DATALOCATOR_URI:
+        FALLTHROUGH_INTENDED;
     case XA_DATALOCATOR_ANDROIDFD:
         break;
 
@@ -378,11 +379,13 @@
     case XA_DATALOCATOR_ANDROIDBUFFERQUEUE:
         mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE;
         break;
-    case XA_DATALOCATOR_URI: // intended fall-through
+    case XA_DATALOCATOR_URI:
+        FALLTHROUGH_INTENDED;
     case SL_DATALOCATOR_ANDROIDFD:
         mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
         break;
-    case XA_DATALOCATOR_ADDRESS: // intended fall-through
+    case XA_DATALOCATOR_ADDRESS:
+        FALLTHROUGH_INTENDED;
     default:
         mp->mAndroidObjType = INVALID_TYPE;
         SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
@@ -446,7 +449,8 @@
         }
         }
         break;
-    case INVALID_TYPE: // intended fall-through
+    case INVALID_TYPE:
+        FALLTHROUGH_INTENDED;
     default:
         SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
         result = XA_RESULT_PARAMETER_INVALID;
@@ -534,7 +538,8 @@
         }
     } break;
 
-    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
+    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
     default:
         *pDurMsec = XA_TIME_UNKNOWN;
         break;
@@ -551,7 +556,8 @@
 
     switch (avp->mAndroidObjType) {
 
-    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
+    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
     case AUDIOVIDEOPLAYER_FROM_URIFD: {
         int pos = ANDROID_UNKNOWN_TIME;
         if (avp->mAVPlayer != 0) {
@@ -631,9 +637,9 @@
          case ANDROID_UNINITIALIZED:
              *pObjState = ANDROID_PREPARING;
              gp->prepare();
-             // intended fall through
+             FALLTHROUGH_INTENDED;
          case ANDROID_PREPARING:
-             // intended fall through
+             FALLTHROUGH_INTENDED;
          case ANDROID_READY:
              gp->play();
              break;
@@ -663,7 +669,8 @@
             mp->mAVPlayer->seek(posMsec);
         }
         break;
-      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
+      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
       default: {
           result = XA_RESULT_FEATURE_UNSUPPORTED;
       }
@@ -683,7 +690,8 @@
             mp->mAVPlayer->loop(loopEnable);
         }
         break;
-      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
+      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
+        FALLTHROUGH_INTENDED;
       default: {
           result = XA_RESULT_FEATURE_UNSUPPORTED;
       }
@@ -746,7 +754,7 @@
             result = SL_RESULT_SUCCESS;
             } break;
         case NATIVE_WINDOW_FRAMEBUFFER:              // FramebufferNativeWindow
-            // fall through
+            FALLTHROUGH_INTENDED;
         default:
             SL_LOGE("ANativeWindow * %p has unknown or unsupported concrete type %d",
                     nativeWindow, value);
diff --git a/src/android/android_GenericPlayer.cpp b/src/android/android_GenericPlayer.cpp
index 3a8960c..b3afc1b 100644
--- a/src/android/android_GenericPlayer.cpp
+++ b/src/android/android_GenericPlayer.cpp
@@ -71,7 +71,7 @@
     }
 
     mLooper->registerHandler(this);
-    mLooper->start(false /*runOnCallingThread*/, false /*canCallJava*/, PRIORITY_DEFAULT);
+    mLooper->start(false /*runOnCallingThread*/, true /*canCallJava*/, PRIORITY_DEFAULT);
 }
 
 
diff --git a/src/data.cpp b/src/data.cpp
index ace2862..fc6dffc 100644
--- a/src/data.cpp
+++ b/src/data.cpp
@@ -364,6 +364,7 @@
                 break;
             }
             // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
+            FALLTHROUGH_INTENDED;
         case SL_DATAFORMAT_PCM:
             pDataFormat->mPCM = *(SLDataFormat_PCM *)pFormat;
             do {
@@ -790,7 +791,7 @@
     // Per the spec, the pFormat field is ignored in some cases
     case SL_DATALOCATOR_IODEVICE:
         myDataSrc.pFormat = NULL;
-        // fall through
+        FALLTHROUGH_INTENDED;
     case SL_DATALOCATOR_NULL:
     case SL_DATALOCATOR_MIDIBUFFERQUEUE:
         allowedDataFormatMask &= DATAFORMAT_MASK_NULL;
@@ -872,7 +873,7 @@
     case SL_DATALOCATOR_OUTPUTMIX:
     case XA_DATALOCATOR_NATIVEDISPLAY:
         myDataSink.pFormat = NULL;
-        // fall through
+        FALLTHROUGH_INTENDED;
     case SL_DATALOCATOR_NULL:
     case SL_DATALOCATOR_MIDIBUFFERQUEUE:
         allowedDataFormatMask &= DATAFORMAT_MASK_NULL;
diff --git a/src/itf/I3DDoppler.cpp b/src/itf/I3DDoppler.cpp
index 8a20f81..174bd75 100644
--- a/src/itf/I3DDoppler.cpp
+++ b/src/itf/I3DDoppler.cpp
@@ -81,7 +81,7 @@
                 break;
             case CARTESIAN_UNKNOWN_SPHERICAL_SET:
                 thiz->mVelocityActive = CARTESIAN_REQUESTED_SPHERICAL_SET;
-                // fall through
+                FALLTHROUGH_INTENDED;
             case CARTESIAN_REQUESTED_SPHERICAL_SET:
                 // matched by cond_broadcast in case multiple requesters
 #if 0
diff --git a/src/itf/I3DLocation.cpp b/src/itf/I3DLocation.cpp
index c0eff3b..d323232 100644
--- a/src/itf/I3DLocation.cpp
+++ b/src/itf/I3DLocation.cpp
@@ -87,7 +87,7 @@
                 break;
             case CARTESIAN_UNKNOWN_SPHERICAL_SET:
                 thiz->mLocationActive = CARTESIAN_REQUESTED_SPHERICAL_SET;
-                // fall through
+                FALLTHROUGH_INTENDED;
             case CARTESIAN_REQUESTED_SPHERICAL_SET:
                 // matched by cond_broadcast in case multiple requesters
 #if 0
@@ -134,7 +134,7 @@
                 break;
             case CARTESIAN_UNKNOWN_SPHERICAL_SET:
                 thiz->mLocationActive = CARTESIAN_REQUESTED_SPHERICAL_SET;
-                // fall through
+                FALLTHROUGH_INTENDED;
             case CARTESIAN_REQUESTED_SPHERICAL_SET:
                 // matched by cond_broadcast in case multiple requesters
 #if 0
diff --git a/src/itf/I3DMacroscopic.cpp b/src/itf/I3DMacroscopic.cpp
index feb91e2..01da236 100644
--- a/src/itf/I3DMacroscopic.cpp
+++ b/src/itf/I3DMacroscopic.cpp
@@ -170,7 +170,7 @@
                 break;
             case ANGLES_SET_VECTORS_UNKNOWN:
                 thiz->mOrientationActive = ANGLES_SET_VECTORS_REQUESTED;
-                // fall through
+                FALLTHROUGH_INTENDED;
             case ANGLES_SET_VECTORS_REQUESTED:
                 // matched by cond_broadcast in case multiple requesters
 #if 0
diff --git a/src/itf/IDeviceVolume.cpp b/src/itf/IDeviceVolume.cpp
index bd9f0be..58e10ff 100644
--- a/src/itf/IDeviceVolume.cpp
+++ b/src/itf/IDeviceVolume.cpp
@@ -55,7 +55,7 @@
     case DEVICE_ID_HEADSET:
     case DEVICE_ID_HANDSFREE:
         deviceID = SL_DEFAULTDEVICEID_AUDIOOUTPUT;
-        // fall through
+        FALLTHROUGH_INTENDED;
     case SL_DEFAULTDEVICEID_AUDIOINPUT:
     case SL_DEFAULTDEVICEID_AUDIOOUTPUT:
         {
@@ -87,7 +87,7 @@
         case DEVICE_ID_HEADSET:
         case DEVICE_ID_HANDSFREE:
             deviceID = SL_DEFAULTDEVICEID_AUDIOOUTPUT;
-            // fall through
+            FALLTHROUGH_INTENDED;
         case SL_DEFAULTDEVICEID_AUDIOINPUT:
         case SL_DEFAULTDEVICEID_AUDIOOUTPUT:
             {
diff --git a/src/itf/IObject.cpp b/src/itf/IObject.cpp
index befe939..da88845 100644
--- a/src/itf/IObject.cpp
+++ b/src/itf/IObject.cpp
@@ -534,7 +534,7 @@
             break;
         case predestroy_error:
             SL_LOGE("Object::Destroy(%p) not allowed", thiz);
-            // fall through
+            FALLTHROUGH_INTENDED;
         case predestroy_again:
             object_unlock_exclusive(thiz);
             // unfortunately Destroy doesn't return a result
@@ -601,7 +601,7 @@
             }
             *interfaceStateP = INTERFACE_INITIALIZED;
             }
-            // fall through
+            FALLTHROUGH_INTENDED;
         case INTERFACE_INITIALIZED:
             {
             VoidHook deinit = MPH_init_table[x->mMPH].mDeinit;
@@ -629,11 +629,10 @@
     // The mutex is unlocked and destroyed by IObject_deinit, which is the last deinitializer
     memset(thiz, 0x55, clazz->mSize); // catch broken applications that continue using interfaces
                                         // was ifdef USE_DEBUG but safer to do this unconditionally
-    free(thiz);
-
     if (SL_OBJECTID_ENGINE == clazz->mSLObjectID) {
         CEngine_Destroyed((CEngine *) thiz);
     }
+    free(thiz);
 
     SL_LEAVE_INTERFACE_VOID
 }
diff --git a/src/itf/IPlay.cpp b/src/itf/IPlay.cpp
index 8e44463..acee3ff 100644
--- a/src/itf/IPlay.cpp
+++ b/src/itf/IPlay.cpp
@@ -61,7 +61,7 @@
                     // note that USE_OUTPUTMIXEXT does not support ATTR_ABQ_ENQUEUE
                     attr |= ATTR_BQ_ENQUEUE;
                 }
-                // fall through
+                FALLTHROUGH_INTENDED;
 
             case (SL_PLAYSTATE_STOPPED  << 2) | SL_PLAYSTATE_PAUSED:
             case (SL_PLAYSTATE_PLAYING  << 2) | SL_PLAYSTATE_PAUSED:
diff --git a/src/sles_allinclusive.h b/src/sles_allinclusive.h
index ede6210..0e6d09a 100644
--- a/src/sles_allinclusive.h
+++ b/src/sles_allinclusive.h
@@ -400,6 +400,14 @@
 extern void audioPlayerTransportUpdate(CAudioPlayer *audioPlayer);
 #endif
 
+#ifndef FALLTHROUGH_INTENDED
+#ifdef __clang__
+#define FALLTHROUGH_INTENDED [[clang::fallthrough]]
+#else
+#define FALLTHROUGH_INTENDED
+#endif // __clang__
+#endif // FALLTHROUGH_INTENDED
+
 extern SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size);
 extern SLresult IBufferQueue_Clear(SLBufferQueueItf self);
 extern SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self,