Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8538e159 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "cas: convert MediaCas to HIDL"

parents cd1038cf d5a416a4
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -5,26 +5,14 @@
cc_library_shared {
    name: "libmediadrm",

    aidl: {
        local_include_dirs: ["aidl"],
        export_aidl_headers: true,
    },

    srcs: [
        "aidl/android/media/ICas.aidl",
        "aidl/android/media/ICasListener.aidl",
        "aidl/android/media/IDescrambler.aidl",
        "aidl/android/media/IMediaCasService.aidl",

        "CasImpl.cpp",
        "DescramblerImpl.cpp",
        "DrmPluginPath.cpp",
        "DrmSessionManager.cpp",
        "ICrypto.cpp",
        "IDrm.cpp",
        "IDrmClient.cpp",
        "IMediaDrmService.cpp",
        "MediaCasDefs.cpp",
        "SharedLibrary.cpp",
        "DrmHal.cpp",
        "CryptoHal.cpp",

drm/libmediadrm/CasImpl.cpp

deleted100644 → 0
+0 −224
Original line number Diff line number Diff line

/*
 * Copyright (C) 2017 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 "CasImpl"

#include <android/media/ICasListener.h>
#include <media/cas/CasAPI.h>
#include <media/CasImpl.h>
#include <media/SharedLibrary.h>
#include <utils/Log.h>

namespace android {

static Status getBinderStatus(status_t err) {
    if (err == OK) {
        return Status::ok();
    }
    if (err == BAD_VALUE) {
        return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
    }
    if (err == INVALID_OPERATION) {
        return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
    }
    return Status::fromServiceSpecificError(err);
}

static String8 sessionIdToString(const CasSessionId &sessionId) {
    String8 result;
    for (size_t i = 0; i < sessionId.size(); i++) {
        result.appendFormat("%02x ", sessionId[i]);
    }
    if (result.isEmpty()) {
        result.append("(null)");
    }
    return result;
}

struct CasImpl::PluginHolder : public RefBase {
public:
    explicit PluginHolder(CasPlugin *plugin) : mPlugin(plugin) {}
    ~PluginHolder() { if (mPlugin != NULL) delete mPlugin; }
    CasPlugin* get() { return mPlugin; }

private:
    CasPlugin *mPlugin;
    DISALLOW_EVIL_CONSTRUCTORS(PluginHolder);
};

CasImpl::CasImpl(const sp<ICasListener> &listener)
    : mPluginHolder(NULL), mListener(listener) {
    ALOGV("CTOR");
}

CasImpl::~CasImpl() {
    ALOGV("DTOR");
    release();
}

//static
void CasImpl::OnEvent(
        void *appData,
        int32_t event,
        int32_t arg,
        uint8_t *data,
        size_t size) {
    if (appData == NULL) {
        ALOGE("Invalid appData!");
        return;
    }
    CasImpl *casImpl = static_cast<CasImpl *>(appData);
    casImpl->onEvent(event, arg, data, size);
}

void CasImpl::init(const sp<SharedLibrary>& library, CasPlugin *plugin) {
    mLibrary = library;
    mPluginHolder = new PluginHolder(plugin);
}

void CasImpl::onEvent(
        int32_t event, int32_t arg, uint8_t *data, size_t size) {
    if (mListener == NULL) {
        return;
    }

    std::unique_ptr<CasData> eventData;
    if (data != NULL && size > 0) {
        eventData.reset(new CasData(data, data + size));
    }

    mListener->onEvent(event, arg, eventData);
}

Status CasImpl::setPrivateData(const CasData& pvtData) {
    ALOGV("setPrivateData");
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }
    return getBinderStatus(holder->get()->setPrivateData(pvtData));
}

Status CasImpl::openSession(CasSessionId* sessionId) {
    ALOGV("openSession");
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }
    status_t err = holder->get()->openSession(sessionId);

    ALOGV("openSession: session opened, sessionId=%s",
            sessionIdToString(*sessionId).string());

    return getBinderStatus(err);
}

Status CasImpl::setSessionPrivateData(
        const CasSessionId &sessionId, const CasData& pvtData) {
    ALOGV("setSessionPrivateData: sessionId=%s",
            sessionIdToString(sessionId).string());
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }
    return getBinderStatus(holder->get()->setSessionPrivateData(sessionId, pvtData));
}

Status CasImpl::closeSession(const CasSessionId &sessionId) {
    ALOGV("closeSession: sessionId=%s",
            sessionIdToString(sessionId).string());
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }
    return getBinderStatus(holder->get()->closeSession(sessionId));
}

Status CasImpl::processEcm(const CasSessionId &sessionId, const ParcelableCasData& ecm) {
    ALOGV("processEcm: sessionId=%s",
            sessionIdToString(sessionId).string());
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }

    return getBinderStatus(holder->get()->processEcm(sessionId, ecm));
}

Status CasImpl::processEmm(const ParcelableCasData& emm) {
    ALOGV("processEmm");
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }

    return getBinderStatus(holder->get()->processEmm(emm));
}

Status CasImpl::sendEvent(
        int32_t event, int32_t arg, const ::std::unique_ptr<CasData> &eventData) {
    ALOGV("sendEvent");
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }

    status_t err;
    if (eventData == nullptr) {
        err = holder->get()->sendEvent(event, arg, CasData());
    } else {
        err = holder->get()->sendEvent(event, arg, *eventData);
    }
    return getBinderStatus(err);
}

Status CasImpl::provision(const String16& provisionString) {
    ALOGV("provision: provisionString=%s", String8(provisionString).string());
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }

    return getBinderStatus(holder->get()->provision(String8(provisionString)));
}

Status CasImpl::refreshEntitlements(
        int32_t refreshType, const ::std::unique_ptr<CasData> &refreshData) {
    ALOGV("refreshEntitlements");
    sp<PluginHolder> holder = mPluginHolder;
    if (holder == NULL) {
        return getBinderStatus(INVALID_OPERATION);
    }

    status_t err;
    if (refreshData == nullptr) {
        err = holder->get()->refreshEntitlements(refreshType, CasData());
    } else {
        err = holder->get()->refreshEntitlements(refreshType, *refreshData);
    }
    return getBinderStatus(err);
}

Status CasImpl::release() {
    ALOGV("release: plugin=%p",
            mPluginHolder == NULL ? mPluginHolder->get() : NULL);
    mPluginHolder.clear();
    return Status::ok();
}

} // namespace android
+0 −107
Original line number Diff line number Diff line

/*
 * Copyright (C) 2017 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 "DescramblerImpl"

#include <media/cas/DescramblerAPI.h>
#include <media/DescramblerImpl.h>
#include <media/SharedLibrary.h>
#include <utils/Log.h>
#include <binder/IMemory.h>

namespace android {

static Status getBinderStatus(status_t err) {
    if (err == OK) {
        return Status::ok();
    }
    if (err == BAD_VALUE) {
        return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
    }
    if (err == INVALID_OPERATION) {
        return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
    }
    return Status::fromServiceSpecificError(err);
}

static String8 sessionIdToString(const CasSessionId &sessionId) {
    String8 result;
    for (size_t i = 0; i < sessionId.size(); i++) {
        result.appendFormat("%02x ", sessionId[i]);
    }
    if (result.isEmpty()) {
        result.append("(null)");
    }
    return result;
}

DescramblerImpl::DescramblerImpl(
        const sp<SharedLibrary>& library, DescramblerPlugin *plugin) :
        mLibrary(library), mPlugin(plugin) {
    ALOGV("CTOR: mPlugin=%p", mPlugin);
}

DescramblerImpl::~DescramblerImpl() {
    ALOGV("DTOR: mPlugin=%p", mPlugin);
    release();
}

Status DescramblerImpl::setMediaCasSession(const CasSessionId& sessionId) {
    ALOGV("setMediaCasSession: sessionId=%s",
            sessionIdToString(sessionId).string());

    return getBinderStatus(mPlugin->setMediaCasSession(sessionId));
}

Status DescramblerImpl::requiresSecureDecoderComponent(
        const String16& mime, bool *result) {
    *result = mPlugin->requiresSecureDecoderComponent(String8(mime));

    return getBinderStatus(OK);
}

Status DescramblerImpl::descramble(
        const DescrambleInfo& info, int32_t *result) {
    ALOGV("descramble");

    *result = mPlugin->descramble(
            info.dstType != DescrambleInfo::kDestinationTypeVmPointer,
            info.scramblingControl,
            info.numSubSamples,
            info.subSamples,
            info.srcMem->pointer(),
            info.srcOffset,
            info.dstType == DescrambleInfo::kDestinationTypeVmPointer ?
                    info.srcMem->pointer() : info.dstPtr,
            info.dstOffset,
            NULL);

    return getBinderStatus(*result >= 0 ? OK : *result);
}

Status DescramblerImpl::release() {
    ALOGV("release: mPlugin=%p", mPlugin);

    if (mPlugin != NULL) {
        delete mPlugin;
        mPlugin = NULL;
    }
    return Status::ok();
}

} // namespace android

drm/libmediadrm/MediaCasDefs.cpp

deleted100644 → 0
+0 −184
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 "MediaCas"

#include <media/MediaCasDefs.h>
#include <utils/Log.h>
#include <binder/IMemory.h>

namespace android {
namespace media {

///////////////////////////////////////////////////////////////////////////////
namespace MediaCas {

status_t ParcelableCasData::readFromParcel(const Parcel* parcel) {
    return parcel->readByteVector(this);
}

status_t ParcelableCasData::writeToParcel(Parcel* parcel) const  {
    return parcel->writeByteVector(*this);
}

///////////////////////////////////////////////////////////////////////////////

status_t ParcelableCasPluginDescriptor::readFromParcel(const Parcel* /*parcel*/) {
    ALOGE("CAPluginDescriptor::readFromParcel() shouldn't be called");
    return INVALID_OPERATION;
}

status_t ParcelableCasPluginDescriptor::writeToParcel(Parcel* parcel) const {
    status_t err = parcel->writeInt32(mCASystemId);
    if (err != NO_ERROR) {
        return err;
    }
    return parcel->writeString16(mName);
}

} // namespace MediaCas
///////////////////////////////////////////////////////////////////////////////

namespace MediaDescrambler {

DescrambleInfo::DescrambleInfo() {}

DescrambleInfo::~DescrambleInfo() {}

status_t DescrambleInfo::readFromParcel(const Parcel* parcel) {
    status_t err = parcel->readInt32((int32_t*)&dstType);
    if (err != OK) {
        return err;
    }
    if (dstType != kDestinationTypeNativeHandle
            && dstType != kDestinationTypeVmPointer) {
        return BAD_VALUE;
    }

    err = parcel->readInt32((int32_t*)&scramblingControl);
    if (err != OK) {
        return err;
    }

    err = parcel->readUint32((uint32_t*)&numSubSamples);
    if (err != OK) {
        return err;
    }
    if (numSubSamples > 0xffff) {
        return BAD_VALUE;
    }

    subSamples = new DescramblerPlugin::SubSample[numSubSamples];
    if (subSamples == NULL) {
        return NO_MEMORY;
    }

    for (size_t i = 0; i < numSubSamples; i++) {
        err = parcel->readUint32(&subSamples[i].mNumBytesOfClearData);
        if (err != OK) {
            return err;
        }
        err = parcel->readUint32(&subSamples[i].mNumBytesOfEncryptedData);
        if (err != OK) {
            return err;
        }
    }

    srcMem = interface_cast<IMemory>(parcel->readStrongBinder());
    if (srcMem == NULL) {
        return BAD_VALUE;
    }

    err = parcel->readInt32(&srcOffset);
    if (err != OK) {
        return err;
    }

    native_handle_t *nativeHandle = NULL;
    if (dstType == kDestinationTypeNativeHandle) {
        nativeHandle = parcel->readNativeHandle();
        dstPtr = static_cast<void *>(nativeHandle);
    } else {
        dstPtr = NULL;
    }

    err = parcel->readInt32(&dstOffset);
    if (err != OK) {
        return err;
    }

    return OK;
}

status_t DescrambleInfo::writeToParcel(Parcel* parcel) const {
    if (dstType != kDestinationTypeNativeHandle
            && dstType != kDestinationTypeVmPointer) {
        return BAD_VALUE;
    }

    status_t err = parcel->writeInt32((int32_t)dstType);
    if (err != OK) {
        return err;
    }

    err = parcel->writeInt32(scramblingControl);
    if (err != OK) {
        return err;
    }

    err = parcel->writeUint32(numSubSamples);
    if (err != OK) {
        return err;
    }

    for (size_t i = 0; i < numSubSamples; i++) {
        err = parcel->writeUint32(subSamples[i].mNumBytesOfClearData);
        if (err != OK) {
            return err;
        }
        err = parcel->writeUint32(subSamples[i].mNumBytesOfEncryptedData);
        if (err != OK) {
            return err;
        }
    }

    err = parcel->writeStrongBinder(IInterface::asBinder(srcMem));
    if (err != OK) {
        return err;
    }

    err = parcel->writeInt32(srcOffset);
    if (err != OK) {
        return err;
    }

    if (dstType == kDestinationTypeNativeHandle) {
        parcel->writeNativeHandle(static_cast<native_handle_t *>(dstPtr));
    }

    err = parcel->writeInt32(dstOffset);
    if (err != OK) {
        return err;
    }

    return OK;
}

} // namespace MediaDescrambler

} // namespace media
} // namespace android
+0 −33
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.
 */

package android.media;

import android.media.MediaCas;

/** @hide */
interface ICas {
    void setPrivateData(in byte[] pvtData);
    byte[] openSession();
    void closeSession(in byte[] sessionId);
    void setSessionPrivateData(in byte[] sessionId, in byte[] pvtData);
    void processEcm(in byte[] sessionId, in MediaCas.ParcelableCasData ecm);
    void processEmm(in MediaCas.ParcelableCasData emm);
    void sendEvent(int event, int arg, in @nullable byte[] eventData);
    void provision(String provisionString);
    void refreshEntitlements(int refreshType, in @nullable byte[] refreshData);
    void release();
}
 No newline at end of file
Loading