1
2/*
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17//#define LOG_NDEBUG 0
18#define LOG_TAG "DescramblerImpl"
19
20#include <media/cas/DescramblerAPI.h>
21#include <media/DescramblerImpl.h>
22#include <media/SharedLibrary.h>
23#include <utils/Log.h>
24#include <binder/IMemory.h>
25
26namespace android {
27
28static Status getBinderStatus(status_t err) {
29    if (err == OK) {
30        return Status::ok();
31    }
32    if (err == BAD_VALUE) {
33        return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
34    }
35    if (err == INVALID_OPERATION) {
36        return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE);
37    }
38    return Status::fromServiceSpecificError(err);
39}
40
41static String8 sessionIdToString(const CasSessionId &sessionId) {
42    String8 result;
43    for (size_t i = 0; i < sessionId.size(); i++) {
44        result.appendFormat("%02x ", sessionId[i]);
45    }
46    if (result.isEmpty()) {
47        result.append("(null)");
48    }
49    return result;
50}
51
52DescramblerImpl::DescramblerImpl(
53        const sp<SharedLibrary>& library, DescramblerPlugin *plugin) :
54        mLibrary(library), mPlugin(plugin) {
55    ALOGV("CTOR: mPlugin=%p", mPlugin);
56}
57
58DescramblerImpl::~DescramblerImpl() {
59    ALOGV("DTOR: mPlugin=%p", mPlugin);
60    release();
61}
62
63Status DescramblerImpl::setMediaCasSession(const CasSessionId& sessionId) {
64    ALOGV("setMediaCasSession: sessionId=%s",
65            sessionIdToString(sessionId).string());
66
67    return getBinderStatus(mPlugin->setMediaCasSession(sessionId));
68}
69
70Status DescramblerImpl::requiresSecureDecoderComponent(
71        const String16& mime, bool *result) {
72    *result = mPlugin->requiresSecureDecoderComponent(String8(mime));
73
74    return getBinderStatus(OK);
75}
76
77Status DescramblerImpl::descramble(
78        const DescrambleInfo& info, int32_t *result) {
79    ALOGV("descramble");
80
81    *result = mPlugin->descramble(
82            info.dstType != DescrambleInfo::kDestinationTypeVmPointer,
83            info.scramblingControl,
84            info.numSubSamples,
85            info.subSamples,
86            info.srcMem->pointer(),
87            info.srcOffset,
88            info.dstType == DescrambleInfo::kDestinationTypeVmPointer ?
89                    info.srcMem->pointer() : info.dstPtr,
90            info.dstOffset,
91            NULL);
92
93    return getBinderStatus(*result >= 0 ? OK : *result);
94}
95
96Status DescramblerImpl::release() {
97    ALOGV("release: mPlugin=%p", mPlugin);
98
99    if (mPlugin != NULL) {
100        delete mPlugin;
101        mPlugin = NULL;
102    }
103    return Status::ok();
104}
105
106} // namespace android
107
108