HDCP.cpp revision be996645b3118b84edf26fc05cc5e4ff56c163b5
1/*
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
39    typedef HDCPModule *(*CreateHDCPModuleFunc)(
40            void *, HDCPModule::ObserverFunc);
41
42    CreateHDCPModuleFunc createHDCPModule =
43        (CreateHDCPModuleFunc)dlsym(mLibHandle, "createHDCPModule");
44
45    if (createHDCPModule == NULL) {
46        ALOGE("Unable to find symbol 'createHDCPModule'.");
47    } else if ((mHDCPModule = createHDCPModule(
48                    this, &HDCP::ObserveWrapper)) == NULL) {
49        ALOGE("createHDCPModule failed.");
50    }
51}
52
53HDCP::~HDCP() {
54    Mutex::Autolock autoLock(mLock);
55
56    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) {
68    Mutex::Autolock autoLock(mLock);
69
70    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) {
80    Mutex::Autolock autoLock(mLock);
81
82    if (mHDCPModule == NULL) {
83        return NO_INIT;
84    }
85
86    return mHDCPModule->initAsync(host, port);
87}
88
89status_t HDCP::shutdownAsync() {
90    Mutex::Autolock autoLock(mLock);
91
92    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) {
102    Mutex::Autolock autoLock(mLock);
103
104    if (mHDCPModule == NULL) {
105        *outInputCTR = 0;
106
107        return NO_INIT;
108    }
109
110    return mHDCPModule->encrypt(inData, size, streamCTR, outInputCTR, outData);
111}
112
113// 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) {
119    Mutex::Autolock autoLock(mLock);
120
121    if (mObserver != NULL) {
122        mObserver->notify(msg, ext1, ext2, NULL /* obj */);
123    }
124}
125
126}  // namespace android
127
128