blob: c2ac1a3b30021778da87d18ee060ffc0119bd502 [file] [log] [blame]
Andreas Huber1b19c9d2012-08-29 11:34:22 -07001/*
2 * Copyright (C) 2012 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 "HDCP"
19#include <utils/Log.h>
20
21#include "HDCP.h"
22
23#include <media/stagefright/foundation/ADebug.h>
24
25#include <dlfcn.h>
26
27namespace android {
28
Andreas Hubera6a88d92013-01-30 10:41:25 -080029HDCP::HDCP(bool createEncryptionModule)
30 : mIsEncryptionModule(createEncryptionModule),
31 mLibHandle(NULL),
Andreas Huber1b19c9d2012-08-29 11:34:22 -070032 mHDCPModule(NULL) {
33 mLibHandle = dlopen("libstagefright_hdcp.so", RTLD_NOW);
34
35 if (mLibHandle == NULL) {
36 ALOGE("Unable to locate libstagefright_hdcp.so");
37 return;
38 }
39
Andreas Huberefbb7812012-09-18 10:36:32 -070040 typedef HDCPModule *(*CreateHDCPModuleFunc)(
41 void *, HDCPModule::ObserverFunc);
42
Andreas Huber1b19c9d2012-08-29 11:34:22 -070043 CreateHDCPModuleFunc createHDCPModule =
Andreas Hubera6a88d92013-01-30 10:41:25 -080044 mIsEncryptionModule
45 ? (CreateHDCPModuleFunc)dlsym(mLibHandle, "createHDCPModule")
46 : (CreateHDCPModuleFunc)dlsym(
47 mLibHandle, "createHDCPModuleForDecryption");
Andreas Huber1b19c9d2012-08-29 11:34:22 -070048
49 if (createHDCPModule == NULL) {
50 ALOGE("Unable to find symbol 'createHDCPModule'.");
Andreas Huberefbb7812012-09-18 10:36:32 -070051 } else if ((mHDCPModule = createHDCPModule(
52 this, &HDCP::ObserveWrapper)) == NULL) {
Andreas Huber1b19c9d2012-08-29 11:34:22 -070053 ALOGE("createHDCPModule failed.");
54 }
55}
56
57HDCP::~HDCP() {
Andreas Huberef7d3792012-09-27 14:13:05 -070058 Mutex::Autolock autoLock(mLock);
59
Andreas Huber1b19c9d2012-08-29 11:34:22 -070060 if (mHDCPModule != NULL) {
61 delete mHDCPModule;
62 mHDCPModule = NULL;
63 }
64
65 if (mLibHandle != NULL) {
66 dlclose(mLibHandle);
67 mLibHandle = NULL;
68 }
69}
70
71status_t HDCP::setObserver(const sp<IHDCPObserver> &observer) {
Andreas Huberef7d3792012-09-27 14:13:05 -070072 Mutex::Autolock autoLock(mLock);
73
Andreas Huber1b19c9d2012-08-29 11:34:22 -070074 if (mHDCPModule == NULL) {
75 return NO_INIT;
76 }
77
78 mObserver = observer;
79
80 return OK;
81}
82
83status_t HDCP::initAsync(const char *host, unsigned port) {
Andreas Huberef7d3792012-09-27 14:13:05 -070084 Mutex::Autolock autoLock(mLock);
85
Andreas Huber1b19c9d2012-08-29 11:34:22 -070086 if (mHDCPModule == NULL) {
87 return NO_INIT;
88 }
89
90 return mHDCPModule->initAsync(host, port);
91}
92
93status_t HDCP::shutdownAsync() {
Andreas Huberef7d3792012-09-27 14:13:05 -070094 Mutex::Autolock autoLock(mLock);
95
Andreas Huber1b19c9d2012-08-29 11:34:22 -070096 if (mHDCPModule == NULL) {
97 return NO_INIT;
98 }
99
100 return mHDCPModule->shutdownAsync();
101}
102
Chong Zhangec3acca2013-09-03 14:35:37 -0700103uint32_t HDCP::getCaps() {
104 Mutex::Autolock autoLock(mLock);
105
106 if (mHDCPModule == NULL) {
107 return NO_INIT;
108 }
109
110 // TO-DO:
111 // Only support HDCP_CAPS_ENCRYPT (byte-array to byte-array) for now.
112 // use mHDCPModule->getCaps() when the HDCP libraries get updated.
113 //return mHDCPModule->getCaps();
114 return HDCPModule::HDCP_CAPS_ENCRYPT;
115}
116
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700117status_t HDCP::encrypt(
118 const void *inData, size_t size, uint32_t streamCTR,
119 uint64_t *outInputCTR, void *outData) {
Andreas Huberef7d3792012-09-27 14:13:05 -0700120 Mutex::Autolock autoLock(mLock);
121
Andreas Hubera6a88d92013-01-30 10:41:25 -0800122 CHECK(mIsEncryptionModule);
123
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700124 if (mHDCPModule == NULL) {
125 *outInputCTR = 0;
126
127 return NO_INIT;
128 }
129
130 return mHDCPModule->encrypt(inData, size, streamCTR, outInputCTR, outData);
131}
132
Chong Zhang308bcaa2013-05-03 21:54:17 -0700133status_t HDCP::encryptNative(
134 const sp<GraphicBuffer> &graphicBuffer,
135 size_t offset, size_t size, uint32_t streamCTR,
136 uint64_t *outInputCTR, void *outData) {
137 Mutex::Autolock autoLock(mLock);
138
139 CHECK(mIsEncryptionModule);
140
141 if (mHDCPModule == NULL) {
142 *outInputCTR = 0;
143
144 return NO_INIT;
145 }
146
147 return mHDCPModule->encryptNative(graphicBuffer->handle,
148 offset, size, streamCTR, outInputCTR, outData);
149}
150
Andreas Hubera6a88d92013-01-30 10:41:25 -0800151status_t HDCP::decrypt(
152 const void *inData, size_t size,
153 uint32_t streamCTR, uint64_t outInputCTR, void *outData) {
154 Mutex::Autolock autoLock(mLock);
155
156 CHECK(!mIsEncryptionModule);
157
158 if (mHDCPModule == NULL) {
159 return NO_INIT;
160 }
161
162 return mHDCPModule->decrypt(inData, size, streamCTR, outInputCTR, outData);
163}
164
Andreas Huberefbb7812012-09-18 10:36:32 -0700165// static
166void HDCP::ObserveWrapper(void *me, int msg, int ext1, int ext2) {
167 static_cast<HDCP *>(me)->observe(msg, ext1, ext2);
168}
169
170void HDCP::observe(int msg, int ext1, int ext2) {
Andreas Huberef7d3792012-09-27 14:13:05 -0700171 Mutex::Autolock autoLock(mLock);
172
Andreas Huberefbb7812012-09-18 10:36:32 -0700173 if (mObserver != NULL) {
174 mObserver->notify(msg, ext1, ext2, NULL /* obj */);
175 }
176}
177
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700178} // namespace android
179