Merge "Move MediaDefs from libmedia to libstagefright_foundation"
diff --git a/media/extractors/mp4/ItemTable.cpp b/media/extractors/mp4/ItemTable.cpp
index 88b4bb7..ec15c47 100644
--- a/media/extractors/mp4/ItemTable.cpp
+++ b/media/extractors/mp4/ItemTable.cpp
@@ -1409,7 +1409,14 @@
     meta->setInt32(kKeyWidth, image->width);
     meta->setInt32(kKeyHeight, image->height);
     if (image->rotation != 0) {
-        meta->setInt32(kKeyRotation, image->rotation);
+        // Rotation angle in HEIF is CCW, convert to CW here to be
+        // consistent with the other media formats.
+        switch(image->rotation) {
+            case 90: meta->setInt32(kKeyRotation, 270); break;
+            case 180: meta->setInt32(kKeyRotation, 180); break;
+            case 270: meta->setInt32(kKeyRotation, 90); break;
+            default: break; // don't set if invalid
+        }
     }
     meta->setInt32(kKeyMaxInputSize, image->width * image->height * 1.5);
 
diff --git a/media/libmediaplayerservice/Android.mk b/media/libmediaplayerservice/Android.mk
index 06c2106..630f601 100644
--- a/media/libmediaplayerservice/Android.mk
+++ b/media/libmediaplayerservice/Android.mk
@@ -61,7 +61,7 @@
 
 LOCAL_32_BIT_ONLY := true
 
-LOCAL_SANITIZE := cfi
+LOCAL_SANITIZE := cfi integer_overflow
 LOCAL_SANITIZE_DIAG := cfi
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/media/libstagefright/MediaExtractor.cpp b/media/libstagefright/MediaExtractor.cpp
index e7e42b2..cc2fa63 100644
--- a/media/libstagefright/MediaExtractor.cpp
+++ b/media/libstagefright/MediaExtractor.cpp
@@ -154,19 +154,17 @@
 
     CreatorFunc creator = NULL;
     String8 tmp;
-    if (mime == NULL) {
-        float confidence;
-        creator = sniff(source, &tmp, &confidence, &meta);
-        if (!creator) {
-            ALOGV("FAILED to autodetect media content.");
-            return NULL;
-        }
-
-        mime = tmp.string();
-        ALOGV("Autodetected media content as '%s' with confidence %.2f",
-             mime, confidence);
+    float confidence;
+    creator = sniff(source, &tmp, &confidence, &meta);
+    if (!creator) {
+        ALOGV("FAILED to autodetect media content.");
+        return NULL;
     }
 
+    mime = tmp.string();
+    ALOGV("Autodetected media content as '%s' with confidence %.2f",
+         mime, confidence);
+
     MediaExtractor *ret = creator(source, meta);
 
     if (ret != NULL) {
diff --git a/media/libstagefright/codec2/include/C2Buffer.h b/media/libstagefright/codec2/include/C2Buffer.h
index b5ea381..020fa09 100644
--- a/media/libstagefright/codec2/include/C2Buffer.h
+++ b/media/libstagefright/codec2/include/C2Buffer.h
@@ -154,7 +154,7 @@
      *
      * \retval C2_OK            the fence(s) were successfully signaled
      * \retval C2_BAD_STATE     the fence(s) have already been abandoned or merged (caller error)
-     * \retval C2_ALREADY_EXISTS the fence(s) have already been signaled (caller error)
+     * \retval C2_DUPLICATE     the fence(s) have already been signaled (caller error)
      * \retval C2_NO_PERMISSION no permission to signal the fence (unexpected - system)
      * \retval C2_CORRUPTED     some unknown error prevented signaling the fence(s) (unexpected)
      */
@@ -167,7 +167,7 @@
      *
      * \retval C2_OK            the merging was successfully done
      * \retval C2_NO_MEMORY     not enough memory to perform the merging
-     * \retval C2_ALREADY_EXISTS    the fence have already been merged (caller error)
+     * \retval C2_DUPLICATE     the fence have already been merged (caller error)
      * \retval C2_BAD_STATE     the fence have already been signaled or abandoned (caller error)
      * \retval C2_NO_PERMISSION no permission to merge the fence (unexpected - system)
      * \retval C2_CORRUPTED     some unknown error prevented merging the fence(s) (unexpected)
@@ -182,7 +182,7 @@
      *
      * \retval C2_OK            the fence(s) were successfully signaled
      * \retval C2_BAD_STATE     the fence(s) have already been signaled or merged (caller error)
-     * \retval C2_ALREADY_EXISTS    the fence(s) have already been abandoned (caller error)
+     * \retval C2_DUPLICATE     the fence(s) have already been abandoned (caller error)
      * \retval C2_NO_PERMISSION no permission to abandon the fence (unexpected - system)
      * \retval C2_CORRUPTED     some unknown error prevented signaling the fence(s) (unexpected)
      */
@@ -1379,6 +1379,7 @@
      * \retval C2_OK        the operation was successful
      * \retval C2_NO_PERMISSION no permission to map the portion
      * \retval C2_TIMED_OUT the operation timed out
+     * \retval C2_DUPLICATE if the allocation is already mapped.
      * \retval C2_NO_MEMORY not enough memory to complete the operation
      * \retval C2_BAD_VALUE the parameters (offset/size) are invalid or outside the allocation, or
      *                      the usage flags are invalid (caller error)
@@ -1402,6 +1403,7 @@
      *
      * \retval C2_OK        the operation was successful
      * \retval C2_TIMED_OUT the operation timed out
+     * \retval C2_NOT_FOUND if the allocation was not mapped previously.
      * \retval C2_BAD_VALUE the parameters (addr/size) do not correspond to previously mapped
      *                      regions (caller error)
      * \retval C2_CORRUPTED some unknown error prevented the operation from completing (unexpected)
@@ -1462,7 +1464,7 @@
      *
      * \retval C2_OK        the operation was successful
      * \retval C2_NO_PERMISSION no permission to map the section
-     * \retval C2_ALREADY_EXISTS there is already a mapped region (caller error)
+     * \retval C2_DUPLICATE there is already a mapped region (caller error)
      * \retval C2_TIMED_OUT the operation timed out
      * \retval C2_NO_MEMORY not enough memory to complete the operation
      * \retval C2_BAD_VALUE the parameters (rect) are invalid or outside the allocation, or the
diff --git a/media/libstagefright/codec2/include/C2Component.h b/media/libstagefright/codec2/include/C2Component.h
index 94312fb..f536132 100644
--- a/media/libstagefright/codec2/include/C2Component.h
+++ b/media/libstagefright/codec2/include/C2Component.h
@@ -224,7 +224,7 @@
      *
      * \retval C2_OK        the tunnel was successfully created
      * \retval C2_BAD_INDEX the target component does not exist
-     * \retval C2_ALREADY_EXIST the tunnel already exists
+     * \retval C2_DUPLICATE the tunnel already exists
      * \retval C2_UNSUPPORTED  the tunnel is not supported
      *
      * \retval C2_TIMED_OUT could not create the tunnel within the time limit (unexpected)
diff --git a/media/libstagefright/codec2/include/C2Config.h b/media/libstagefright/codec2/include/C2Config.h
index 18e0a47..b1ac8bc 100644
--- a/media/libstagefright/codec2/include/C2Config.h
+++ b/media/libstagefright/codec2/include/C2Config.h
@@ -72,7 +72,7 @@
     kParamIndexParamStart = 0x800,
 };
 
-C2ENUM(C2DomainKind, int32_t,
+C2ENUM(C2DomainKind, uint32_t,
     C2DomainVideo,
     C2DomainAudio,
     C2DomainOther = C2DomainAudio + 1
diff --git a/media/libstagefright/codec2/include/C2Param.h b/media/libstagefright/codec2/include/C2Param.h
index aab0474..7d0155a 100644
--- a/media/libstagefright/codec2/include/C2Param.h
+++ b/media/libstagefright/codec2/include/C2Param.h
@@ -652,7 +652,7 @@
 
     // constructors - implicit
     template<typename T>
-    C2Value(T value)  : mType(typeFor<T>()),  mValue(value) { }
+    C2Value(T value)  : mType(typeFor<T>()), mValue(value) { }
 
     C2Value() : mType(NO_INIT) { }
 
diff --git a/media/libstagefright/codec2/vndk/include/C2BufferPriv.h b/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
index a536710..b7c752f 100644
--- a/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
+++ b/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 9544e5d..1e88ab2 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -197,7 +197,7 @@
 // Initially this heap is used to allocate client buffers for "fast" AudioRecord.
 // Eventually it will be the single buffer that FastCapture writes into via HAL read(),
 // and that all "fast" AudioRecord clients read from.  In either case, the size can be small.
-static const size_t kRecordThreadReadOnlyHeapSize = 0x2000;
+static const size_t kRecordThreadReadOnlyHeapSize = 0x4000;
 
 // ----------------------------------------------------------------------------
 
diff --git a/services/audiopolicy/common/managerdefinitions/src/SoundTriggerSession.cpp b/services/audiopolicy/common/managerdefinitions/src/SoundTriggerSession.cpp
index 8ca3ae0..0383b0e 100644
--- a/services/audiopolicy/common/managerdefinitions/src/SoundTriggerSession.cpp
+++ b/services/audiopolicy/common/managerdefinitions/src/SoundTriggerSession.cpp
@@ -34,7 +34,7 @@
 {
     ssize_t index = indexOfKey(session);
     if (index < 0) {
-        ALOGW("acquireSoundTriggerSession() session %d not registered", session);
+        ALOGW("releaseSession() session %d not registered", session);
         return BAD_VALUE;
     }
 
diff --git a/services/soundtrigger/SoundTriggerHwService.cpp b/services/soundtrigger/SoundTriggerHwService.cpp
index 952acd7..22519a3 100644
--- a/services/soundtrigger/SoundTriggerHwService.cpp
+++ b/services/soundtrigger/SoundTriggerHwService.cpp
@@ -530,37 +530,45 @@
 void SoundTriggerHwService::Module::detach(const sp<ModuleClient>& moduleClient)
 {
     ALOGV("Module::detach()");
-    AutoMutex lock(mLock);
-    ssize_t index = -1;
+    Vector<audio_session_t> releasedSessions;
 
-    for (size_t i = 0; i < mModuleClients.size(); i++) {
-        if (mModuleClients[i] == moduleClient) {
-            index = i;
-            break;
-        }
-    }
-    if (index == -1) {
-        return;
-    }
+    {
+        AutoMutex lock(mLock);
+        ssize_t index = -1;
 
-    ALOGV("remove client %p", moduleClient.get());
-    mModuleClients.removeAt(index);
-
-    // Iterate in reverse order as models are removed from list inside the loop.
-    for (size_t i = mModels.size(); i > 0; i--) {
-        sp<Model> model = mModels.valueAt(i - 1);
-        if (moduleClient == model->mModuleClient) {
-            mModels.removeItemsAt(i - 1);
-            ALOGV("detach() unloading model %d", model->mHandle);
-            if (mHalInterface != 0) {
-                if (model->mState == Model::STATE_ACTIVE) {
-                    mHalInterface->stopRecognition(model->mHandle);
-                }
-                mHalInterface->unloadSoundModel(model->mHandle);
+        for (size_t i = 0; i < mModuleClients.size(); i++) {
+            if (mModuleClients[i] == moduleClient) {
+                index = i;
+                break;
             }
-            AudioSystem::releaseSoundTriggerSession(model->mCaptureSession);
-            mHalInterface->unloadSoundModel(model->mHandle);
         }
+        if (index == -1) {
+            return;
+        }
+
+        ALOGV("remove client %p", moduleClient.get());
+        mModuleClients.removeAt(index);
+
+        // Iterate in reverse order as models are removed from list inside the loop.
+        for (size_t i = mModels.size(); i > 0; i--) {
+            sp<Model> model = mModels.valueAt(i - 1);
+            if (moduleClient == model->mModuleClient) {
+                mModels.removeItemsAt(i - 1);
+                ALOGV("detach() unloading model %d", model->mHandle);
+                if (mHalInterface != 0) {
+                    if (model->mState == Model::STATE_ACTIVE) {
+                        mHalInterface->stopRecognition(model->mHandle);
+                    }
+                    mHalInterface->unloadSoundModel(model->mHandle);
+                }
+                releasedSessions.add(model->mCaptureSession);
+            }
+        }
+    }
+
+    for (size_t i = 0; i < releasedSessions.size(); i++) {
+        // do not call AudioSystem methods with mLock held
+        AudioSystem::releaseSoundTriggerSession(releasedSessions[i]);
     }
 }
 
@@ -595,61 +603,71 @@
         return BAD_VALUE;
     }
 
-    AutoMutex lock(mLock);
-
-    if (mModels.size() >= mDescriptor.properties.max_sound_models) {
-        ALOGW("loadSoundModel(): Not loading, max number of models (%d) would be exceeded",
-              mDescriptor.properties.max_sound_models);
-        return INVALID_OPERATION;
-    }
-
-    status_t status = mHalInterface->loadSoundModel(sound_model,
-                                                  SoundTriggerHwService::soundModelCallback,
-                                                  this, handle);
-
-    if (status != NO_ERROR) {
-        return status;
-    }
     audio_session_t session;
     audio_io_handle_t ioHandle;
     audio_devices_t device;
-
-    status = AudioSystem::acquireSoundTriggerSession(&session, &ioHandle, &device);
+    // do not call AudioSystem methods with mLock held
+    status_t status = AudioSystem::acquireSoundTriggerSession(&session, &ioHandle, &device);
     if (status != NO_ERROR) {
         return status;
     }
 
-    sp<Model> model = new Model(*handle, session, ioHandle, device, sound_model->type,
-                                moduleClient);
-    mModels.replaceValueFor(*handle, model);
+    {
+        AutoMutex lock(mLock);
 
+        if (mModels.size() >= mDescriptor.properties.max_sound_models) {
+            ALOGW("loadSoundModel(): Not loading, max number of models (%d) would be exceeded",
+                  mDescriptor.properties.max_sound_models);
+            status = INVALID_OPERATION;
+            goto exit;
+        }
+
+        status = mHalInterface->loadSoundModel(sound_model,
+                                                      SoundTriggerHwService::soundModelCallback,
+                                                      this, handle);
+        if (status != NO_ERROR) {
+            goto exit;
+        }
+
+        sp<Model> model = new Model(*handle, session, ioHandle, device, sound_model->type,
+                                    moduleClient);
+        mModels.replaceValueFor(*handle, model);
+    }
+exit:
+    if (status != NO_ERROR) {
+        // do not call AudioSystem methods with mLock held
+        AudioSystem::releaseSoundTriggerSession(session);
+    }
     return status;
 }
 
 status_t SoundTriggerHwService::Module::unloadSoundModel(sound_model_handle_t handle)
 {
     ALOGV("unloadSoundModel() model handle %d", handle);
-    AutoMutex lock(mLock);
-    return unloadSoundModel_l(handle);
-}
+    status_t status;
+    audio_session_t session;
 
-status_t SoundTriggerHwService::Module::unloadSoundModel_l(sound_model_handle_t handle)
-{
-    if (mHalInterface == 0) {
-        return NO_INIT;
+    {
+        AutoMutex lock(mLock);
+        if (mHalInterface == 0) {
+            return NO_INIT;
+        }
+        ssize_t index = mModels.indexOfKey(handle);
+        if (index < 0) {
+            return BAD_VALUE;
+        }
+        sp<Model> model = mModels.valueAt(index);
+        mModels.removeItem(handle);
+        if (model->mState == Model::STATE_ACTIVE) {
+            mHalInterface->stopRecognition(model->mHandle);
+            model->mState = Model::STATE_IDLE;
+        }
+        status = mHalInterface->unloadSoundModel(handle);
+        session = model->mCaptureSession;
     }
-    ssize_t index = mModels.indexOfKey(handle);
-    if (index < 0) {
-        return BAD_VALUE;
-    }
-    sp<Model> model = mModels.valueAt(index);
-    mModels.removeItem(handle);
-    if (model->mState == Model::STATE_ACTIVE) {
-        mHalInterface->stopRecognition(model->mHandle);
-        model->mState = Model::STATE_IDLE;
-    }
-    AudioSystem::releaseSoundTriggerSession(model->mCaptureSession);
-    return mHalInterface->unloadSoundModel(handle);
+    // do not call AudioSystem methods with mLock held
+    AudioSystem::releaseSoundTriggerSession(session);
+    return status;
 }
 
 status_t SoundTriggerHwService::Module::startRecognition(sound_model_handle_t handle,
diff --git a/services/soundtrigger/SoundTriggerHwService.h b/services/soundtrigger/SoundTriggerHwService.h
index 60ebb35..95efc4b 100644
--- a/services/soundtrigger/SoundTriggerHwService.h
+++ b/services/soundtrigger/SoundTriggerHwService.h
@@ -140,8 +140,6 @@
 
     private:
 
-        status_t unloadSoundModel_l(sound_model_handle_t handle);
-
         Mutex                                  mLock;
         wp<SoundTriggerHwService>              mService;
         sp<SoundTriggerHalInterface>           mHalInterface;