1/*
2 * Copyright (C) 2014 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 "ClearKeyCryptoPlugin"
19#include <utils/Log.h>
20
21#include <media/stagefright/MediaErrors.h>
22#include <utils/StrongPointer.h>
23
24#include "DrmPlugin.h"
25
26#include "Session.h"
27
28namespace clearkeydrm {
29
30using android::sp;
31
32status_t DrmPlugin::openSession(Vector<uint8_t>& sessionId) {
33    sp<Session> session = mSessionLibrary->createSession();
34    sessionId = session->sessionId();
35    return android::OK;
36}
37
38status_t DrmPlugin::closeSession(const Vector<uint8_t>& sessionId) {
39    sp<Session> session = mSessionLibrary->findSession(sessionId);
40    mSessionLibrary->destroySession(session);
41    return android::OK;
42}
43
44status_t DrmPlugin::getKeyRequest(
45        const Vector<uint8_t>& scope,
46        const Vector<uint8_t>& initData,
47        const String8& initDataType,
48        KeyType keyType,
49        const KeyedVector<String8, String8>& optionalParameters,
50        Vector<uint8_t>& request,
51        String8& defaultUrl) {
52    UNUSED(optionalParameters);
53    if (keyType != kKeyType_Streaming) {
54        return android::ERROR_DRM_CANNOT_HANDLE;
55    }
56
57    sp<Session> session = mSessionLibrary->findSession(scope);
58    defaultUrl.clear();
59    return session->getKeyRequest(initData, initDataType, &request);
60}
61
62status_t DrmPlugin::provideKeyResponse(
63        const Vector<uint8_t>& scope,
64        const Vector<uint8_t>& response,
65        Vector<uint8_t>& keySetId) {
66    sp<Session> session = mSessionLibrary->findSession(scope);
67    status_t res = session->provideKeyResponse(response);
68    if (res == android::OK) {
69        keySetId.clear();
70    }
71    return res;
72}
73
74status_t DrmPlugin::getPropertyString(
75        const String8& name, String8& value) const {
76    if (name == "vendor") {
77        value = "Google";
78    } else if (name == "version") {
79        value = "1.0";
80    } else if (name == "description") {
81        value = "ClearKey CDM";
82    } else if (name == "algorithms") {
83        value = "";
84    } else {
85        ALOGE("App requested unknown string property %s", name.string());
86        return android::ERROR_DRM_CANNOT_HANDLE;
87    }
88    return android::OK;
89}
90
91}  // namespace clearkeydrm
92