Home | History | Annotate | Download | only in default
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "android.hardware.cas (at) 1.0-DescramblerImpl"
     19 
     20 #include <hidlmemory/mapping.h>
     21 #include <media/hardware/CryptoAPI.h>
     22 #include <media/cas/DescramblerAPI.h>
     23 #include <utils/Log.h>
     24 
     25 #include "DescramblerImpl.h"
     26 #include "SharedLibrary.h"
     27 #include "TypeConvert.h"
     28 
     29 namespace android {
     30 using hidl::memory::V1_0::IMemory;
     31 
     32 namespace hardware {
     33 namespace cas {
     34 namespace V1_0 {
     35 namespace implementation {
     36 
     37 #define CHECK_SUBSAMPLE_DEF(type) \
     38 static_assert(sizeof(SubSample) == sizeof(type::SubSample), \
     39         "SubSample: size doesn't match"); \
     40 static_assert(offsetof(SubSample, numBytesOfClearData) \
     41         == offsetof(type::SubSample, mNumBytesOfClearData), \
     42         "SubSample: numBytesOfClearData offset doesn't match"); \
     43 static_assert(offsetof(SubSample, numBytesOfEncryptedData) \
     44         == offsetof(type::SubSample, mNumBytesOfEncryptedData), \
     45         "SubSample: numBytesOfEncryptedData offset doesn't match")
     46 
     47 CHECK_SUBSAMPLE_DEF(DescramblerPlugin);
     48 CHECK_SUBSAMPLE_DEF(CryptoPlugin);
     49 
     50 DescramblerImpl::DescramblerImpl(
     51         const sp<SharedLibrary>& library, DescramblerPlugin *plugin) :
     52         mLibrary(library), mPlugin(plugin) {
     53     ALOGV("CTOR: mPlugin=%p", mPlugin);
     54 }
     55 
     56 DescramblerImpl::~DescramblerImpl() {
     57     ALOGV("DTOR: mPlugin=%p", mPlugin);
     58     release();
     59 }
     60 
     61 Return<Status> DescramblerImpl::setMediaCasSession(const HidlCasSessionId& sessionId) {
     62     ALOGV("%s: sessionId=%s", __FUNCTION__,
     63             sessionIdToString(sessionId).string());
     64 
     65     return toStatus(mPlugin->setMediaCasSession(sessionId));
     66 }
     67 
     68 Return<bool> DescramblerImpl::requiresSecureDecoderComponent(
     69         const hidl_string& mime) {
     70     return mPlugin->requiresSecureDecoderComponent(String8(mime.c_str()));
     71 }
     72 
     73 Return<void> DescramblerImpl::descramble(
     74         ScramblingControl scramblingControl,
     75         const hidl_vec<SubSample>& subSamples,
     76         const SharedBuffer& srcBuffer,
     77         uint64_t srcOffset,
     78         const DestinationBuffer& dstBuffer,
     79         uint64_t dstOffset,
     80         descramble_cb _hidl_cb) {
     81     ALOGV("%s", __FUNCTION__);
     82 
     83     sp<IMemory> srcMem = mapMemory(srcBuffer.heapBase);
     84     void *srcPtr = (uint8_t *)(void *)srcMem->getPointer() + srcBuffer.offset;
     85     void *dstPtr = NULL;
     86     if (dstBuffer.type == BufferType::SHARED_MEMORY) {
     87         // When using shared memory, src buffer is also used as dst,
     88         // we don't map it again here.
     89         dstPtr = srcPtr;
     90     } else {
     91         native_handle_t *handle = const_cast<native_handle_t *>(
     92                 dstBuffer.secureMemory.getNativeHandle());
     93         dstPtr = static_cast<void *>(handle);
     94     }
     95     // Casting hidl SubSample to DescramblerPlugin::SubSample, but need
     96     // to ensure structs are actually idential
     97 
     98     int32_t result = mPlugin->descramble(
     99             dstBuffer.type != BufferType::SHARED_MEMORY,
    100             (DescramblerPlugin::ScramblingControl)scramblingControl,
    101             subSamples.size(),
    102             (DescramblerPlugin::SubSample*)subSamples.data(),
    103             srcPtr,
    104             srcOffset,
    105             dstPtr,
    106             dstOffset,
    107             NULL);
    108 
    109     _hidl_cb(toStatus(result >= 0 ? OK : result), result, NULL);
    110     return Void();
    111 }
    112 
    113 Return<Status> DescramblerImpl::release() {
    114     ALOGV("%s: mPlugin=%p", __FUNCTION__, mPlugin);
    115 
    116     if (mPlugin != NULL) {
    117         delete mPlugin;
    118         mPlugin = NULL;
    119     }
    120     return Status::OK;
    121 }
    122 
    123 } // namespace implementation
    124 } // namespace V1_0
    125 } // namespace cas
    126 } // namespace hardware
    127 } // namespace android
    128