blob: fc1780d0ea869ff2b6bf4c3637706727a792d8a8 [file] [log] [blame]
Jeff Tinkera53d6552017-01-20 00:31:46 -08001/*
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 "CryptoHal"
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080019#include <mediadrm/CryptoHal.h>
Kyle Zhang6605add2022-01-13 17:51:23 +000020#include <mediadrm/CryptoHalHidl.h>
21#include <mediadrm/CryptoHalAidl.h>
Robert Shih5944a0b2021-02-10 04:26:33 -080022#include <mediadrm/DrmUtils.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080023
Jeff Tinkera53d6552017-01-20 00:31:46 -080024namespace android {
25
Kyle Zhang6605add2022-01-13 17:51:23 +000026CryptoHal::CryptoHal() {
27 mCryptoHalAidl = sp<CryptoHalAidl>::make();
28 mCryptoHalHidl = sp<CryptoHalHidl>::make();
Jeff Tinkera53d6552017-01-20 00:31:46 -080029}
30
Kyle Zhang6605add2022-01-13 17:51:23 +000031CryptoHal::~CryptoHal() {}
Jeff Tinkera53d6552017-01-20 00:31:46 -080032
33status_t CryptoHal::initCheck() const {
Kyle Zhang6605add2022-01-13 17:51:23 +000034 if (mCryptoHalAidl->initCheck() == OK || mCryptoHalHidl->initCheck() == OK) return OK;
35 if (mCryptoHalAidl->initCheck() == NO_INIT || mCryptoHalHidl->initCheck() == NO_INIT)
36 return NO_INIT;
37 return mCryptoHalHidl->initCheck();
Jeff Tinkera53d6552017-01-20 00:31:46 -080038}
39
Jeff Tinkera53d6552017-01-20 00:31:46 -080040bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
Kyle Zhang6605add2022-01-13 17:51:23 +000041 return mCryptoHalAidl->isCryptoSchemeSupported(uuid) ||
42 mCryptoHalHidl->isCryptoSchemeSupported(uuid);
Jeff Tinkera53d6552017-01-20 00:31:46 -080043}
44
Kyle Zhang6605add2022-01-13 17:51:23 +000045status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void* data, size_t size) {
46 if (mCryptoHalAidl->createPlugin(uuid, data, size) != OK)
47 return mCryptoHalHidl->createPlugin(uuid, data, size);
48 return OK;
Jeff Tinkera53d6552017-01-20 00:31:46 -080049}
50
51status_t CryptoHal::destroyPlugin() {
Kyle Zhang6605add2022-01-13 17:51:23 +000052 // This requires plugin to be created.
53 if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->destroyPlugin();
54 return mCryptoHalHidl->destroyPlugin();
Jeff Tinkera53d6552017-01-20 00:31:46 -080055}
56
Kyle Zhang6605add2022-01-13 17:51:23 +000057bool CryptoHal::requiresSecureDecoderComponent(const char* mime) const {
58 // This requires plugin to be created.
59 if (mCryptoHalAidl->initCheck() == OK)
60 return mCryptoHalAidl->requiresSecureDecoderComponent(mime);
61 return mCryptoHalHidl->requiresSecureDecoderComponent(mime);
Jeff Tinkera53d6552017-01-20 00:31:46 -080062}
63
64void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
Kyle Zhang6605add2022-01-13 17:51:23 +000065 // This requires plugin to be created.
66 if (mCryptoHalAidl->initCheck() == OK) {
67 mCryptoHalAidl->notifyResolution(width, height);
Jeff Tinkera53d6552017-01-20 00:31:46 -080068 return;
69 }
70
Kyle Zhang6605add2022-01-13 17:51:23 +000071 mCryptoHalHidl->notifyResolution(width, height);
Jeff Tinkera53d6552017-01-20 00:31:46 -080072}
73
Sohail Nagaraj5a48d1e2022-12-20 10:44:46 +053074DrmStatus CryptoHal::setMediaDrmSession(const Vector<uint8_t>& sessionId) {
Kyle Zhang6605add2022-01-13 17:51:23 +000075 // This requires plugin to be created.
76 if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->setMediaDrmSession(sessionId);
77 return mCryptoHalHidl->setMediaDrmSession(sessionId);
78}
Jeff Tinkera53d6552017-01-20 00:31:46 -080079
Kyle Zhang6605add2022-01-13 17:51:23 +000080ssize_t CryptoHal::decrypt(const uint8_t key[16], const uint8_t iv[16], CryptoPlugin::Mode mode,
81 const CryptoPlugin::Pattern& pattern, const ::SharedBuffer& source,
82 size_t offset, const CryptoPlugin::SubSample* subSamples,
83 size_t numSubSamples, const ::DestinationBuffer& destination,
84 AString* errorDetailMsg) {
85 // This requires plugin to be created.
86 if (mCryptoHalAidl->initCheck() == OK)
87 return mCryptoHalAidl->decrypt(key, iv, mode, pattern, source, offset, subSamples,
88 numSubSamples, destination, errorDetailMsg);
89 return mCryptoHalHidl->decrypt(key, iv, mode, pattern, source, offset, subSamples,
90 numSubSamples, destination, errorDetailMsg);
91}
92
93int32_t CryptoHal::setHeap(const sp<HidlMemory>& heap) {
94 // This requires plugin to be created.
95 if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->setHeap(heap);
96 return mCryptoHalHidl->setHeap(heap);
97}
98
99void CryptoHal::unsetHeap(int32_t seqNum) {
100 // This requires plugin to be created.
101 if (mCryptoHalAidl->initCheck() == OK) {
102 mCryptoHalAidl->unsetHeap(seqNum);
103 return;
Jeff Tinkera53d6552017-01-20 00:31:46 -0800104 }
105
Kyle Zhang6605add2022-01-13 17:51:23 +0000106 mCryptoHalHidl->unsetHeap(seqNum);
Jeff Tinkera53d6552017-01-20 00:31:46 -0800107}
108
Kyle Zhang6605add2022-01-13 17:51:23 +0000109status_t CryptoHal::getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const {
110 // This requires plugin to be created.
111 if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->getLogMessages(logs);
112 return mCryptoHalHidl->getLogMessages(logs);
Robert Shih9afca952021-02-12 01:36:06 -0800113}
Kyle Zhang6605add2022-01-13 17:51:23 +0000114
115} // namespace android