blob: 09b9719dd662a8a17d3203553cf2e03fab9a1318 [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
29HDCP::HDCP()
30 : mLibHandle(NULL),
31 mHDCPModule(NULL) {
32 mLibHandle = dlopen("libstagefright_hdcp.so", RTLD_NOW);
33
34 if (mLibHandle == NULL) {
35 ALOGE("Unable to locate libstagefright_hdcp.so");
36 return;
37 }
38
Andreas Huberefbb7812012-09-18 10:36:32 -070039 typedef HDCPModule *(*CreateHDCPModuleFunc)(
40 void *, HDCPModule::ObserverFunc);
41
Andreas Huber1b19c9d2012-08-29 11:34:22 -070042 CreateHDCPModuleFunc createHDCPModule =
43 (CreateHDCPModuleFunc)dlsym(mLibHandle, "createHDCPModule");
44
45 if (createHDCPModule == NULL) {
46 ALOGE("Unable to find symbol 'createHDCPModule'.");
Andreas Huberefbb7812012-09-18 10:36:32 -070047 } else if ((mHDCPModule = createHDCPModule(
48 this, &HDCP::ObserveWrapper)) == NULL) {
Andreas Huber1b19c9d2012-08-29 11:34:22 -070049 ALOGE("createHDCPModule failed.");
50 }
51}
52
53HDCP::~HDCP() {
Andreas Huberef7d3792012-09-27 14:13:05 -070054 Mutex::Autolock autoLock(mLock);
55
Andreas Huber1b19c9d2012-08-29 11:34:22 -070056 if (mHDCPModule != NULL) {
57 delete mHDCPModule;
58 mHDCPModule = NULL;
59 }
60
61 if (mLibHandle != NULL) {
62 dlclose(mLibHandle);
63 mLibHandle = NULL;
64 }
65}
66
67status_t HDCP::setObserver(const sp<IHDCPObserver> &observer) {
Andreas Huberef7d3792012-09-27 14:13:05 -070068 Mutex::Autolock autoLock(mLock);
69
Andreas Huber1b19c9d2012-08-29 11:34:22 -070070 if (mHDCPModule == NULL) {
71 return NO_INIT;
72 }
73
74 mObserver = observer;
75
76 return OK;
77}
78
79status_t HDCP::initAsync(const char *host, unsigned port) {
Andreas Huberef7d3792012-09-27 14:13:05 -070080 Mutex::Autolock autoLock(mLock);
81
Andreas Huber1b19c9d2012-08-29 11:34:22 -070082 if (mHDCPModule == NULL) {
83 return NO_INIT;
84 }
85
86 return mHDCPModule->initAsync(host, port);
87}
88
89status_t HDCP::shutdownAsync() {
Andreas Huberef7d3792012-09-27 14:13:05 -070090 Mutex::Autolock autoLock(mLock);
91
Andreas Huber1b19c9d2012-08-29 11:34:22 -070092 if (mHDCPModule == NULL) {
93 return NO_INIT;
94 }
95
96 return mHDCPModule->shutdownAsync();
97}
98
99status_t HDCP::encrypt(
100 const void *inData, size_t size, uint32_t streamCTR,
101 uint64_t *outInputCTR, void *outData) {
Andreas Huberef7d3792012-09-27 14:13:05 -0700102 Mutex::Autolock autoLock(mLock);
103
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700104 if (mHDCPModule == NULL) {
105 *outInputCTR = 0;
106
107 return NO_INIT;
108 }
109
110 return mHDCPModule->encrypt(inData, size, streamCTR, outInputCTR, outData);
111}
112
Andreas Huberefbb7812012-09-18 10:36:32 -0700113// static
114void HDCP::ObserveWrapper(void *me, int msg, int ext1, int ext2) {
115 static_cast<HDCP *>(me)->observe(msg, ext1, ext2);
116}
117
118void HDCP::observe(int msg, int ext1, int ext2) {
Andreas Huberef7d3792012-09-27 14:13:05 -0700119 Mutex::Autolock autoLock(mLock);
120
Andreas Huberefbb7812012-09-18 10:36:32 -0700121 if (mObserver != NULL) {
122 mObserver->notify(msg, ext1, ext2, NULL /* obj */);
123 }
124}
125
Andreas Huber1b19c9d2012-08-29 11:34:22 -0700126} // namespace android
127