Initial contribution from Sony Corporation.

  Add DRM Framework to support DRM content playback
  together with StageFright.

  - DRM Framework code is added
     - include/drm
     - drm
  - api/current.xml is updated to include DRM Framework Java APIs
  - cmds/servicemanager/service_manager.c is modified
    to add drmManager and drmIOService.

Change-Id: I6d7bc9c7067362b500e530988a9ce241761866fb
diff --git a/drm/drmserver/Android.mk b/drm/drmserver/Android.mk
new file mode 100644
index 0000000..d2ebe8e
--- /dev/null
+++ b/drm/drmserver/Android.mk
@@ -0,0 +1,39 @@
+#
+# Copyright (C) 2010 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.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+    main_drmserver.cpp \
+    DrmManager.cpp \
+    DrmManagerService.cpp \
+    StringTokenizer.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+    libutils \
+    libbinder \
+    libdl
+
+LOCAL_STATIC_LIBRARIES := libdrmframeworkcommon
+
+LOCAL_C_INCLUDES := \
+    $(TOP)/frameworks/base/include \
+    $(TOP)/frameworks/base/drm/libdrmframework/include \
+    $(TOP)/frameworks/base/drm/libdrmframework/plugins/common/include
+
+LOCAL_MODULE:= drmserver
+
+include $(BUILD_EXECUTABLE)
diff --git a/drm/drmserver/DrmManager.cpp b/drm/drmserver/DrmManager.cpp
new file mode 100644
index 0000000..44886f9
--- /dev/null
+++ b/drm/drmserver/DrmManager.cpp
@@ -0,0 +1,473 @@
+/*
+ * Copyright (C) 2010 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_NDEBUG 0
+#define LOG_TAG "DrmManager(Native)"
+#include "utils/Log.h"
+
+#include <utils/String8.h>
+#include <drm/DrmInfo.h>
+#include <drm/DrmInfoEvent.h>
+#include <drm/DrmRights.h>
+#include <drm/DrmConstraints.h>
+#include <drm/DrmInfoStatus.h>
+#include <drm/DrmInfoRequest.h>
+#include <drm/DrmSupportInfo.h>
+#include <drm/DrmConvertedStatus.h>
+#include <IDrmEngine.h>
+
+#include "DrmManager.h"
+#include "ReadWriteUtils.h"
+
+#define DECRYPT_FILE_ERROR -1
+
+using namespace android;
+
+const String8 DrmManager::EMPTY_STRING("");
+
+DrmManager::DrmManager() :
+    mDecryptSessionId(0),
+    mConvertId(0) {
+
+}
+
+DrmManager::~DrmManager() {
+
+}
+
+status_t DrmManager::loadPlugIns(int uniqueId) {
+    String8 pluginDirPath("/system/lib/drm/plugins/native");
+    return loadPlugIns(uniqueId, pluginDirPath);
+}
+
+status_t DrmManager::loadPlugIns(int uniqueId, const String8& plugInDirPath) {
+    if (mSupportInfoToPlugInIdMap.isEmpty()) {
+        mPlugInManager.loadPlugIns(plugInDirPath);
+
+        initializePlugIns(uniqueId);
+
+        populate(uniqueId);
+    } else {
+        initializePlugIns(uniqueId);
+    }
+
+    return DRM_NO_ERROR;
+}
+
+status_t DrmManager::setDrmServiceListener(
+            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
+    Mutex::Autolock _l(mLock);
+    mServiceListeners.add(uniqueId, drmServiceListener);
+    return DRM_NO_ERROR;
+}
+
+status_t DrmManager::unloadPlugIns(int uniqueId) {
+    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
+
+    for (unsigned int index = 0; index < plugInIdList.size(); index++) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
+        rDrmEngine.terminate(uniqueId);
+    }
+
+    mConvertSessionMap.clear();
+    mDecryptSessionMap.clear();
+    mSupportInfoToPlugInIdMap.clear();
+    mPlugInManager.unloadPlugIns();
+    return DRM_NO_ERROR;
+}
+
+DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
+    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.getConstraints(uniqueId, path, action);
+    }
+    return NULL;
+}
+
+status_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) {
+    mPlugInManager.loadPlugIn(absolutePath);
+
+    IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath);
+    rDrmEngine.initialize(uniqueId);
+    rDrmEngine.setOnInfoListener(uniqueId, this);
+
+    DrmSupportInfo* info = rDrmEngine.getSupportInfo(uniqueId);
+    mSupportInfoToPlugInIdMap.add(*info, absolutePath);
+
+    return DRM_NO_ERROR;
+}
+
+bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
+    const String8 plugInId = getSupportedPlugInId(mimeType);
+    bool result = (EMPTY_STRING != plugInId) ? true : false;
+
+    if (NULL != path) {
+        if (result) {
+            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+            result = rDrmEngine.canHandle(uniqueId, path);
+        } else {
+            result = canHandle(uniqueId, path);
+        }
+    }
+    return result;
+}
+
+DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
+    const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
+    }
+    return NULL;
+}
+
+bool DrmManager::canHandle(int uniqueId, const String8& path) {
+    bool result = false;
+    Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
+
+    for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
+        result = rDrmEngine.canHandle(uniqueId, path);
+
+        if (result) {
+            break;
+        }
+    }
+    return result;
+}
+
+DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
+    const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
+    }
+    return NULL;
+}
+
+void DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
+            const String8& rightsPath, const String8& contentPath) {
+    const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
+    }
+}
+
+String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
+    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.getOriginalMimeType(uniqueId, path);
+    }
+    return EMPTY_STRING;
+}
+
+int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
+    const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
+    }
+    return DrmObjectType::UNKNOWN;
+}
+
+int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
+    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.checkRightsStatus(uniqueId, path, action);
+    }
+    return RightsStatus::RIGHTS_INVALID;
+}
+
+void DrmManager::consumeRights(
+    int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
+    }
+}
+
+void DrmManager::setPlaybackStatus(
+    int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
+
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
+    }
+}
+
+bool DrmManager::validateAction(
+    int uniqueId, const String8& path, int action, const ActionDescription& description) {
+    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        return rDrmEngine.validateAction(uniqueId, path, action, description);
+    }
+    return false;
+}
+
+void DrmManager::removeRights(int uniqueId, const String8& path) {
+    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+        rDrmEngine.removeRights(uniqueId, path);
+    }
+}
+
+void DrmManager::removeAllRights(int uniqueId) {
+    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
+
+    for (unsigned int index = 0; index < plugInIdList.size(); index++) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
+        rDrmEngine.removeAllRights(uniqueId);
+    }
+}
+
+int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
+    int convertId = -1;
+
+    const String8 plugInId = getSupportedPlugInId(mimeType);
+    if (EMPTY_STRING != plugInId) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+
+        Mutex::Autolock _l(mConvertLock);
+        ++mConvertId;
+        convertId = mConvertId;
+        mConvertSessionMap.add(mConvertId, &rDrmEngine);
+
+        rDrmEngine.openConvertSession(uniqueId, mConvertId);
+    }
+    return convertId;
+}
+
+DrmConvertedStatus* DrmManager::convertData(
+            int uniqueId, int convertId, const DrmBuffer* inputData) {
+    DrmConvertedStatus *drmConvertedStatus = NULL;
+
+    if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
+        drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
+    }
+    return drmConvertedStatus;
+}
+
+DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
+    DrmConvertedStatus *drmConvertedStatus = NULL;
+
+    if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
+        drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
+        mConvertSessionMap.removeItem(convertId);
+    }
+    return drmConvertedStatus;
+}
+
+status_t DrmManager::getAllSupportInfo(
+                    int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
+    Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
+    int size = plugInPathList.size();
+    int validPlugins = 0;
+
+    if (0 < size) {
+        Vector<DrmSupportInfo> drmSupportInfoList;
+
+        for (int i = 0; i < size; ++i) {
+            String8 plugInPath = plugInPathList[i];
+            DrmSupportInfo* drmSupportInfo
+                = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(uniqueId);
+            if (NULL != drmSupportInfo) {
+                drmSupportInfoList.add(*drmSupportInfo);
+                delete drmSupportInfo; drmSupportInfo = NULL;
+            }
+        }
+
+        validPlugins = drmSupportInfoList.size();
+        if (0 < validPlugins) {
+            *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
+            for (int i = 0; i < validPlugins; ++i) {
+                (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
+            }
+        }
+    }
+    *length = validPlugins;
+    return DRM_NO_ERROR;
+}
+
+DecryptHandle* DrmManager::openDecryptSession(int uniqueId, int fd, int offset, int length) {
+    LOGV("Entering DrmManager::openDecryptSession");
+    status_t result = DRM_ERROR_CANNOT_HANDLE;
+    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
+
+    DecryptHandle* handle = new DecryptHandle();
+    if (NULL != handle) {
+        Mutex::Autolock _l(mDecryptLock);
+        handle->decryptId = mDecryptSessionId + 1;
+
+        for (unsigned int index = 0; index < plugInIdList.size(); index++) {
+            String8 plugInId = plugInIdList.itemAt(index);
+            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
+            result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length);
+
+            LOGV("plug-in %s return value = %d", plugInId.string(), result);
+
+            if (DRM_NO_ERROR == result) {
+                ++mDecryptSessionId;
+                mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
+                LOGV("plug-in %s is selected", plugInId.string());
+                break;
+            }
+        }
+    }
+
+    if (DRM_ERROR_CANNOT_HANDLE == result) {
+        delete handle; handle = NULL;
+        LOGE("DrmManager::openDecryptSession: no capable plug-in found");
+    }
+
+    return handle;
+}
+
+void DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        drmEngine->closeDecryptSession(uniqueId, decryptHandle);
+
+        mDecryptSessionMap.removeItem(decryptHandle->decryptId);
+    }
+}
+
+void DrmManager::initializeDecryptUnit(
+    int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
+    }
+}
+
+status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle,
+            int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer) {
+    status_t status = DRM_ERROR_UNKNOWN;
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        status = drmEngine->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer);
+    }
+    return status;
+}
+
+void DrmManager::finalizeDecryptUnit(
+            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
+    }
+}
+
+ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
+            void* buffer, ssize_t numBytes, off_t offset) {
+    ssize_t result = DECRYPT_FILE_ERROR;
+
+    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
+        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
+        result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
+    }
+    return result;
+}
+
+void DrmManager::initializePlugIns(int uniqueId) {
+    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
+
+    for (unsigned int index = 0; index < plugInIdList.size(); index++) {
+        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
+        rDrmEngine.initialize(uniqueId);
+        rDrmEngine.setOnInfoListener(uniqueId, this);
+    }
+}
+
+void DrmManager::populate(int uniqueId) {
+    Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
+
+    for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
+        String8 plugInPath = plugInPathList[i];
+        DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(uniqueId);
+        if (NULL != info) {
+            mSupportInfoToPlugInIdMap.add(*info, plugInPath);
+        }
+    }
+}
+
+String8 DrmManager::getSupportedPlugInId(
+            int uniqueId, const String8& path, const String8& mimeType) {
+    String8 plugInId("");
+
+    if (EMPTY_STRING != mimeType) {
+        plugInId = getSupportedPlugInId(mimeType);
+    } else {
+        plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
+    }
+    return plugInId;
+}
+
+String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
+    String8 plugInId("");
+
+    if (EMPTY_STRING != mimeType) {
+        for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
+            const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
+
+            if (drmSupportInfo.isSupportedMimeType(mimeType)) {
+                plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
+                break;
+            }
+        }
+    }
+    return plugInId;
+}
+
+String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
+    String8 plugInId("");
+    const String8 fileSuffix = path.getPathExtension();
+
+    for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
+        const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
+
+        if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
+            String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
+            IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
+
+            if (drmEngine.canHandle(uniqueId, path)) {
+                plugInId = key;
+                break;
+            }
+        }
+    }
+    return plugInId;
+}
+
+void DrmManager::onInfo(const DrmInfoEvent& event) {
+    Mutex::Autolock _l(mLock);
+    for (unsigned int index = 0; index < mServiceListeners.size(); index++) {
+        int uniqueId = mServiceListeners.keyAt(index);
+
+        if (uniqueId == event.getUniqueId()) {
+            sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
+            serviceListener->notify(event);
+        }
+    }
+}
+
diff --git a/drm/drmserver/DrmManagerService.cpp b/drm/drmserver/DrmManagerService.cpp
new file mode 100644
index 0000000..9d000e9
--- /dev/null
+++ b/drm/drmserver/DrmManagerService.cpp
@@ -0,0 +1,218 @@
+/*
+ * Copyright (C) 2010 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_NDEBUG 0
+#define LOG_TAG "DrmManagerService(Native)"
+#include <utils/Log.h>
+
+#include <errno.h>
+#include <utils/threads.h>
+#include <binder/IServiceManager.h>
+#include <sys/stat.h>
+#include "DrmManagerService.h"
+#include "DrmManager.h"
+
+using namespace android;
+
+#define SUCCESS 0
+#define DRM_DIRECTORY_PERMISSION 0700
+
+void DrmManagerService::instantiate() {
+    LOGV("instantiate");
+
+    int res = mkdir("/data/drm/plugins", DRM_DIRECTORY_PERMISSION);

+    if (SUCCESS == res || EEXIST == errno) {

+        res = mkdir("/data/drm/plugins/native", DRM_DIRECTORY_PERMISSION);

+        if (SUCCESS == res || EEXIST == errno) {

+            res = mkdir("/data/drm/plugins/native/databases", DRM_DIRECTORY_PERMISSION);

+            if (SUCCESS == res || EEXIST == errno) {

+                defaultServiceManager()

+                    ->addService(String16("drm.drmManager"), new DrmManagerService());

+            }

+        }

+    }
+}
+
+DrmManagerService::DrmManagerService() {
+    LOGV("created");
+    mDrmManager = NULL;
+    mDrmManager = new DrmManager();
+}
+
+DrmManagerService::~DrmManagerService() {
+    LOGV("Destroyed");
+    delete mDrmManager; mDrmManager = NULL;
+}
+
+status_t DrmManagerService::loadPlugIns(int uniqueId) {
+    LOGV("Entering load plugins");
+    return mDrmManager->loadPlugIns(uniqueId);
+}
+
+status_t DrmManagerService::loadPlugIns(int uniqueId, const String8& plugInDirPath) {
+    LOGV("Entering load plugins from path");
+    return mDrmManager->loadPlugIns(uniqueId, plugInDirPath);
+}
+
+status_t DrmManagerService::setDrmServiceListener(
+            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
+    LOGV("Entering setDrmServiceListener");
+    mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
+    return DRM_NO_ERROR;
+}
+
+status_t DrmManagerService::unloadPlugIns(int uniqueId) {
+    LOGV("Entering unload plugins");
+    return mDrmManager->unloadPlugIns(uniqueId);
+}
+
+status_t DrmManagerService::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
+    LOGV("Entering installDrmEngine");
+    return mDrmManager->installDrmEngine(uniqueId, drmEngineFile);
+}
+
+DrmConstraints* DrmManagerService::getConstraints(
+            int uniqueId, const String8* path, const int action) {
+    LOGV("Entering getConstraints from content");
+    return mDrmManager->getConstraints(uniqueId, path, action);
+}
+
+bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
+    LOGV("Entering canHandle");
+    return mDrmManager->canHandle(uniqueId, path, mimeType);
+}
+
+DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
+    LOGV("Entering processDrmInfo");
+    return mDrmManager->processDrmInfo(uniqueId, drmInfo);
+}
+
+DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
+    LOGV("Entering acquireDrmInfo");
+    return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
+}
+
+void DrmManagerService::saveRights(
+            int uniqueId, const DrmRights& drmRights,
+            const String8& rightsPath, const String8& contentPath) {
+    LOGV("Entering saveRights");
+    return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
+}
+
+String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
+    LOGV("Entering getOriginalMimeType");
+    return mDrmManager->getOriginalMimeType(uniqueId, path);
+}
+
+int DrmManagerService::getDrmObjectType(
+           int uniqueId, const String8& path, const String8& mimeType) {
+    LOGV("Entering getDrmObjectType");
+    return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
+}
+
+int DrmManagerService::checkRightsStatus(
+            int uniqueId, const String8& path, int action) {
+    LOGV("Entering checkRightsStatus");
+    return mDrmManager->checkRightsStatus(uniqueId, path, action);
+}
+
+void DrmManagerService::consumeRights(
+            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
+    LOGV("Entering consumeRights");
+    mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
+}
+
+void DrmManagerService::setPlaybackStatus(
+            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
+    LOGV("Entering setPlaybackStatus");
+    mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
+}
+
+bool DrmManagerService::validateAction(
+            int uniqueId, const String8& path,
+            int action, const ActionDescription& description) {
+    LOGV("Entering validateAction");
+    return mDrmManager->validateAction(uniqueId, path, action, description);
+}
+
+void DrmManagerService::removeRights(int uniqueId, const String8& path) {
+    LOGV("Entering removeRights");
+    mDrmManager->removeRights(uniqueId, path);
+}
+
+void DrmManagerService::removeAllRights(int uniqueId) {
+    LOGV("Entering removeAllRights");
+    mDrmManager->removeAllRights(uniqueId);
+}
+
+int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
+    LOGV("Entering openConvertSession");
+    return mDrmManager->openConvertSession(uniqueId, mimeType);
+}
+
+DrmConvertedStatus* DrmManagerService::convertData(
+            int uniqueId, int convertId, const DrmBuffer* inputData) {
+    LOGV("Entering convertData");
+    return mDrmManager->convertData(uniqueId, convertId, inputData);
+}
+
+DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
+    LOGV("Entering closeConvertSession");
+    return mDrmManager->closeConvertSession(uniqueId, convertId);
+}
+
+status_t DrmManagerService::getAllSupportInfo(
+            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
+    LOGV("Entering getAllSupportInfo");
+    return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
+}
+
+DecryptHandle* DrmManagerService::openDecryptSession(
+            int uniqueId, int fd, int offset, int length) {
+    LOGV("Entering DrmManagerService::openDecryptSession");
+    return mDrmManager->openDecryptSession(uniqueId, fd, offset, length);
+}
+
+void DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
+    LOGV("Entering closeDecryptSession");
+    mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
+}
+
+void DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
+            int decryptUnitId, const DrmBuffer* headerInfo) {
+    LOGV("Entering initializeDecryptUnit");
+    mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
+}
+
+status_t DrmManagerService::decrypt(
+            int uniqueId, DecryptHandle* decryptHandle,
+            int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer) {
+    LOGV("Entering decrypt");
+    return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer);
+}
+
+void DrmManagerService::finalizeDecryptUnit(
+            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
+    LOGV("Entering finalizeDecryptUnit");
+    mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
+}
+
+ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
+            void* buffer, ssize_t numBytes, off_t offset) {
+    LOGV("Entering pread");
+    return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
+}
+
diff --git a/drm/drmserver/StringTokenizer.cpp b/drm/drmserver/StringTokenizer.cpp
new file mode 100644
index 0000000..367c9bd
--- /dev/null
+++ b/drm/drmserver/StringTokenizer.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2010 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "StringTokenizer.h"
+
+using namespace android;
+
+StringTokenizer::StringTokenizer(const String8& string, const String8& delimiter) {
+    splitString(string, delimiter);
+}
+
+void StringTokenizer::splitString(const String8& string, const String8& delimiter) {
+    for (unsigned int i = 0; i < string.length(); i++) {
+        unsigned int position = string.find(delimiter.string(), i);
+        if (string.length() != position) {
+            String8 token(string.string()+i, position-i);
+            if (token.length()) {
+                mStringTokenizerVector.push(token);
+                i = position + delimiter.length() - 1;
+            }
+        } else {
+            mStringTokenizerVector.push(String8(string.string()+i, string.length()-i));
+            break;
+        }
+    }
+}
+
+StringTokenizer::Iterator StringTokenizer::iterator() {
+    return Iterator(this);
+}
+
+StringTokenizer::Iterator::Iterator(const StringTokenizer::Iterator& iterator) :
+    mStringTokenizer(iterator.mStringTokenizer),
+    mIndex(iterator.mIndex) {
+    LOGV("StringTokenizer::Iterator::Iterator");
+}
+
+StringTokenizer::Iterator& StringTokenizer::Iterator::operator=(
+            const StringTokenizer::Iterator& iterator) {
+    LOGV("StringTokenizer::Iterator::operator=");
+    mStringTokenizer = iterator.mStringTokenizer;
+    mIndex = iterator.mIndex;
+    return *this;
+}
+
+bool StringTokenizer::Iterator::hasNext() {
+    return mIndex < mStringTokenizer->mStringTokenizerVector.size();
+}
+
+String8& StringTokenizer::Iterator::next() {
+    String8& value = mStringTokenizer->mStringTokenizerVector.editItemAt(mIndex);
+    mIndex++;
+    return value;
+}
+
diff --git a/drm/drmserver/main_drmserver.cpp b/drm/drmserver/main_drmserver.cpp
new file mode 100644
index 0000000..6d10646
--- /dev/null
+++ b/drm/drmserver/main_drmserver.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2010 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <grp.h>
+
+#include <binder/IPCThreadState.h>
+#include <binder/ProcessState.h>
+#include <binder/IServiceManager.h>
+#include <utils/Log.h>
+#include <private/android_filesystem_config.h>
+
+#include <DrmManagerService.h>
+
+using namespace android;
+
+int main(int argc, char** argv)
+{
+    sp<ProcessState> proc(ProcessState::self());
+    sp<IServiceManager> sm = defaultServiceManager();
+    LOGI("ServiceManager: %p", sm.get());
+    DrmManagerService::instantiate();
+    ProcessState::self()->startThreadPool();
+    IPCThreadState::self()->joinThreadPool();
+}
+