- Change level of some LOGs from LOGE to LOGV because those logs
  are not error logs from non-DRMed content's point of view.
- Add more mutex lock to avoid crash by multi-thread calls.
- Fix for bug 4050039

Change-Id: I52fed22069f0cda6c4ac6a5363469168c797a33b
diff --git a/drm/common/IDrmManagerService.cpp b/drm/common/IDrmManagerService.cpp
index ddbd220..696e305 100644
--- a/drm/common/IDrmManagerService.cpp
+++ b/drm/common/IDrmManagerService.cpp
@@ -596,7 +596,7 @@
             handle->decryptInfo->decryptBufferLength = reply.readInt32();
         }
     } else {
-        LOGE("no decryptHandle is generated in service side");
+        LOGV("no decryptHandle is generated in service side");
     }
     return handle;
 }
@@ -1308,7 +1308,7 @@
                 delete handle->decryptInfo; handle->decryptInfo = NULL;
             }
         } else {
-            LOGE("NULL decryptHandle is returned");
+            LOGV("NULL decryptHandle is returned");
         }
         delete handle; handle = NULL;
         return DRM_NO_ERROR;
diff --git a/drm/drmserver/DrmManager.cpp b/drm/drmserver/DrmManager.cpp
index ec400b7..305bafc 100644
--- a/drm/drmserver/DrmManager.cpp
+++ b/drm/drmserver/DrmManager.cpp
@@ -51,6 +51,7 @@
 }
 
 int DrmManager::addUniqueId(int uniqueId) {
+    Mutex::Autolock _l(mLock);
     if (0 == uniqueId) {
         int temp = 0;
         bool foundUniqueId = false;
@@ -78,6 +79,7 @@
 }
 
 void DrmManager::removeUniqueId(int uniqueId) {
+    Mutex::Autolock _l(mLock);
     for (unsigned int i = 0; i < mUniqueIdVector.size(); i++) {
         if (uniqueId == mUniqueIdVector.itemAt(i)) {
             mUniqueIdVector.removeAt(i);
@@ -107,6 +109,7 @@
 }
 
 status_t DrmManager::unloadPlugIns() {
+    Mutex::Autolock _l(mLock);
     mConvertSessionMap.clear();
     mDecryptSessionMap.clear();
     mPlugInManager.unloadPlugIns();
@@ -126,6 +129,7 @@
 }
 
 void DrmManager::addClient(int uniqueId) {
+    Mutex::Autolock _l(mLock);
     if (!mSupportInfoToPlugInIdMap.isEmpty()) {
         Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
         for (unsigned int index = 0; index < plugInIdList.size(); index++) {
@@ -137,6 +141,7 @@
 }
 
 void DrmManager::removeClient(int uniqueId) {
+    Mutex::Autolock _l(mLock);
     Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
     for (unsigned int index = 0; index < plugInIdList.size(); index++) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
@@ -145,6 +150,7 @@
 }
 
 DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -154,6 +160,7 @@
 }
 
 DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -163,6 +170,7 @@
 }
 
 status_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) {
+    Mutex::Autolock _l(mLock);
     mPlugInManager.loadPlugIn(absolutePath);
 
     IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath);
@@ -176,6 +184,7 @@
 }
 
 bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInId(mimeType);
     bool result = (EMPTY_STRING != plugInId) ? true : false;
 
@@ -194,6 +203,7 @@
 }
 
 DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -218,6 +228,7 @@
 }
 
 DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -228,6 +239,7 @@
 
 status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
     status_t result = DRM_ERROR_UNKNOWN;
     if (EMPTY_STRING != plugInId) {
@@ -238,6 +250,7 @@
 }
 
 String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -247,6 +260,7 @@
 }
 
 int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -256,6 +270,7 @@
 }
 
 int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -267,6 +282,7 @@
 status_t DrmManager::consumeRights(
     int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
     status_t result = DRM_ERROR_UNKNOWN;
+    Mutex::Autolock _l(mDecryptLock);
     if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
         result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
@@ -277,6 +293,7 @@
 status_t DrmManager::setPlaybackStatus(
     int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
     status_t result = DRM_ERROR_UNKNOWN;
+    Mutex::Autolock _l(mDecryptLock);
     if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
         result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
@@ -286,6 +303,7 @@
 
 bool DrmManager::validateAction(
     int uniqueId, const String8& path, int action, const ActionDescription& description) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
@@ -295,6 +313,7 @@
 }
 
 status_t DrmManager::removeRights(int uniqueId, const String8& path) {
+    Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
     status_t result = DRM_ERROR_UNKNOWN;
     if (EMPTY_STRING != plugInId) {
@@ -318,6 +337,7 @@
 }
 
 int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
+    Mutex::Autolock _l(mConvertLock);
     int convertId = -1;
 
     const String8 plugInId = getSupportedPlugInId(mimeType);
@@ -325,7 +345,6 @@
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
 
         if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
-            Mutex::Autolock _l(mConvertLock);
             ++mConvertId;
             convertId = mConvertId;
             mConvertSessionMap.add(convertId, &rDrmEngine);
@@ -338,6 +357,7 @@
             int uniqueId, int convertId, const DrmBuffer* inputData) {
     DrmConvertedStatus *drmConvertedStatus = NULL;
 
+    Mutex::Autolock _l(mConvertLock);
     if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
         drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
@@ -346,6 +366,7 @@
 }
 
 DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
+    Mutex::Autolock _l(mConvertLock);
     DrmConvertedStatus *drmConvertedStatus = NULL;
 
     if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
@@ -358,6 +379,7 @@
 
 status_t DrmManager::getAllSupportInfo(
                     int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
+    Mutex::Autolock _l(mLock);
     Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
     int size = plugInPathList.size();
     int validPlugins = 0;
@@ -437,7 +459,7 @@
     }
     if (DRM_NO_ERROR != result) {
         delete handle; handle = NULL;
-        LOGE("DrmManager::openDecryptSession: no capable plug-in found");
+        LOGV("DrmManager::openDecryptSession: no capable plug-in found");
     }
     return handle;
 }
@@ -458,6 +480,7 @@
 status_t DrmManager::initializeDecryptUnit(
     int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
     status_t result = DRM_ERROR_UNKNOWN;
+    Mutex::Autolock _l(mDecryptLock);
     if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
         result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
@@ -468,6 +491,8 @@
 status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
             const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
     status_t result = DRM_ERROR_UNKNOWN;
+
+    Mutex::Autolock _l(mDecryptLock);
     if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
         result = drmEngine->decrypt(
@@ -479,6 +504,7 @@
 status_t DrmManager::finalizeDecryptUnit(
             int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
     status_t result = DRM_ERROR_UNKNOWN;
+    Mutex::Autolock _l(mDecryptLock);
     if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
         result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
@@ -490,6 +516,7 @@
             void* buffer, ssize_t numBytes, off64_t offset) {
     ssize_t result = DECRYPT_FILE_ERROR;
 
+    Mutex::Autolock _l(mDecryptLock);
     if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
         IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
         result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);