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

Commit 9dbe9a57 authored by Chong Zhang's avatar Chong Zhang
Browse files

MediaCas: add MediaCasService

This CL adds API only without implementation.

bug: 22804304
Change-Id: Ibb5a29cc616ec0af81957b2bfe1419c482591753
parent a146a0c4
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -6,12 +6,24 @@ LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_AIDL_INCLUDES := \
    frameworks/base/media/java

LOCAL_SRC_FILES := \
    ../../../base/media/java/android/media/ICas.aidl \
    ../../../base/media/java/android/media/ICasListener.aidl \
    ../../../base/media/java/android/media/IDescrambler.aidl \
    ../../../base/media/java/android/media/IMediaCasService.aidl \

LOCAL_SRC_FILES += \
    CasImpl.cpp \
    DescramblerImpl.cpp \
    DrmSessionManager.cpp \
    ICrypto.cpp \
    IDrm.cpp \
    IDrmClient.cpp \
    IMediaDrmService.cpp \
    MediaCasDefs.cpp \
    SharedLibrary.cpp
ifneq ($(DISABLE_TREBLE_DRM), true)
LOCAL_SRC_FILES += \
+201 −0
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;
}

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

CasImpl::~CasImpl() {
    ALOGV("DTOR: mPlugin=%p", mPlugin);
    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;
    mPlugin = 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");
    return getBinderStatus(mPlugin->setPrivateData(pvtData));
}

Status CasImpl::openSession(int32_t program_number, CasSessionId* sessionId) {
    ALOGV("openSession: program_number=%d", program_number);

    status_t err = mPlugin->openSession(program_number, sessionId);

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

    return getBinderStatus(err);
}

Status CasImpl::openSessionForStream(
        int32_t program_number,
        int32_t elementary_PID,
        CasSessionId* sessionId) {
    ALOGV("openSession: program_number=%d, elementary_PID=%d",
            program_number, elementary_PID);

    status_t err = mPlugin->openSession(
            program_number, elementary_PID, sessionId);

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

    return getBinderStatus(err);
}

Status CasImpl::setSessionPrivateData(
        const CasSessionId &sessionId, const CasData& pvtData) {
    ALOGV("setSessionPrivateData: sessionId=%s",
            sessionIdToString(sessionId).string());

    return getBinderStatus(mPlugin->setSessionPrivateData(sessionId, pvtData));
}

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

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

Status CasImpl::processEcm(const CasSessionId &sessionId, const ParcelableCasData& ecm) {
    ALOGV("processEcm: sessionId=%s",
            sessionIdToString(sessionId).string());

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

Status CasImpl::processEmm(const ParcelableCasData& emm) {
    ALOGV("processEmm");

    return getBinderStatus(mPlugin->processEmm(emm));
}

Status CasImpl::sendEvent(
        int32_t event, int32_t arg, const ::std::unique_ptr<CasData> &eventData) {
    ALOGV("sendEvent");

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

Status CasImpl::provision(const String16& provisionString) {
    ALOGV("provision: provisionString=%s", String8(provisionString).string());

    return getBinderStatus(mPlugin->provision(String8(provisionString)));
}

Status CasImpl::refreshEntitlements(
        int32_t refreshType, const ::std::unique_ptr<CasData> &refreshData) {
    ALOGV("refreshEntitlements");

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

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

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

} // namespace android
+107 −0
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
+184 −0
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
+3 −0
Original line number Diff line number Diff line
@@ -43,6 +43,9 @@ namespace android {
        if (!mLibHandle) {
            return NULL;
        }
        // Clear last error before we load the symbol again,
        // in case the caller didn't retrieve it.
        (void)dlerror();
        return dlsym(mLibHandle, symbol);
    }

Loading