DrmPassthruPlugIn.cpp revision 2272ee27d9022d173b6eab45c409b3c3f57f30ec
1/*
2 * Copyright (C) 2010 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 "DrmPassthruPlugIn"
19#include <utils/Log.h>
20
21#include <drm/DrmRights.h>
22#include <drm/DrmConstraints.h>
23#include <drm/DrmInfo.h>
24#include <drm/DrmInfoEvent.h>
25#include <drm/DrmInfoStatus.h>
26#include <drm/DrmConvertedStatus.h>
27#include <drm/DrmInfoRequest.h>
28#include <drm/DrmSupportInfo.h>
29#include <DrmPassthruPlugIn.h>
30
31using namespace android;
32
33
34// This extern "C" is mandatory to be managed by TPlugInManager
35extern "C" IDrmEngine* create() {
36    return new DrmPassthruPlugIn();
37}
38
39// This extern "C" is mandatory to be managed by TPlugInManager
40extern "C" void destroy(IDrmEngine* pPlugIn) {
41    delete pPlugIn;
42    pPlugIn = NULL;
43}
44
45DrmPassthruPlugIn::DrmPassthruPlugIn()
46    : DrmEngineBase() {
47
48}
49
50DrmPassthruPlugIn::~DrmPassthruPlugIn() {
51
52}
53
54DrmConstraints* DrmPassthruPlugIn::onGetConstraints(
55        int uniqueId, const String8* path, int action) {
56    LOGD("DrmPassthruPlugIn::onGetConstraints From Path: %d", uniqueId);
57    DrmConstraints* drmConstraints = new DrmConstraints();
58
59    String8 value("dummy_available_time");
60    char* charValue = NULL;
61    charValue = new char[value.length() + 1];
62    strncpy(charValue, value.string(), value.length());
63
64    //Just add dummy available time for verification
65    drmConstraints->put(&(DrmConstraints::LICENSE_AVAILABLE_TIME), charValue);
66
67    return drmConstraints;
68}
69
70DrmInfoStatus* DrmPassthruPlugIn::onProcessDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
71    LOGD("DrmPassthruPlugIn::onProcessDrmInfo - Enter : %d", uniqueId);
72    DrmInfoStatus* drmInfoStatus = NULL;
73    if (NULL != drmInfo) {
74        switch (drmInfo->getInfoType()) {
75        case DrmInfoRequest::TYPE_REGISTRATION_INFO: {
76            const DrmBuffer* emptyBuffer = new DrmBuffer();
77            drmInfoStatus
78                = new DrmInfoStatus(DrmInfoStatus::STATUS_OK, emptyBuffer, drmInfo->getMimeType());
79            break;
80        }
81        case DrmInfoRequest::TYPE_UNREGISTRATION_INFO: {
82            const DrmBuffer* emptyBuffer = new DrmBuffer();
83            drmInfoStatus
84                = new DrmInfoStatus(DrmInfoStatus::STATUS_OK, emptyBuffer, drmInfo->getMimeType());
85            break;
86        }
87        case DrmInfoRequest::TYPE_RIGHTS_ACQUISITION_INFO: {
88            String8 licenseString("dummy_license_string");
89            const int bufferSize = licenseString.size();
90            char* data = NULL;
91            data = new char[bufferSize];
92            memcpy(data, licenseString.string(), bufferSize);
93            const DrmBuffer* buffer = new DrmBuffer(data, bufferSize);
94            drmInfoStatus
95                = new DrmInfoStatus(DrmInfoStatus::STATUS_OK, buffer, drmInfo->getMimeType());
96            break;
97        }
98        }
99    }
100    LOGD("DrmPassthruPlugIn::onProcessDrmInfo - Exit");
101    return drmInfoStatus;
102}
103
104status_t DrmPassthruPlugIn::onSetOnInfoListener(
105            int uniqueId, const IDrmEngine::OnInfoListener* infoListener) {
106    LOGD("DrmPassthruPlugIn::onSetOnInfoListener : %d", uniqueId);
107    return DRM_NO_ERROR;
108}
109
110status_t DrmPassthruPlugIn::onInitialize(int uniqueId) {
111    LOGD("DrmPassthruPlugIn::onInitialize : %d", uniqueId);
112    return DRM_NO_ERROR;
113}
114
115status_t DrmPassthruPlugIn::onTerminate(int uniqueId) {
116    LOGD("DrmPassthruPlugIn::onTerminate : %d", uniqueId);
117    return DRM_NO_ERROR;
118}
119
120DrmSupportInfo* DrmPassthruPlugIn::onGetSupportInfo(int uniqueId) {
121    LOGD("DrmPassthruPlugIn::onGetSupportInfo : %d", uniqueId);
122    DrmSupportInfo* drmSupportInfo = new DrmSupportInfo();
123    // Add mimetype's
124    drmSupportInfo->addMimeType(String8("application/vnd.passthru.drm"));
125    // Add File Suffixes
126    drmSupportInfo->addFileSuffix(String8(".passthru"));
127    // Add plug-in description
128    drmSupportInfo->setDescription(String8("Passthru plug-in"));
129    return drmSupportInfo;
130}
131
132status_t DrmPassthruPlugIn::onSaveRights(int uniqueId, const DrmRights& drmRights,
133            const String8& rightsPath, const String8& contentPath) {
134    LOGD("DrmPassthruPlugIn::onSaveRights : %d", uniqueId);
135    return DRM_NO_ERROR;
136}
137
138DrmInfo* DrmPassthruPlugIn::onAcquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
139    LOGD("DrmPassthruPlugIn::onAcquireDrmInfo : %d", uniqueId);
140    DrmInfo* drmInfo = NULL;
141
142    if (NULL != drmInfoRequest) {
143        String8 dataString("dummy_acquistion_string");
144        int length = dataString.length();
145        char* data = NULL;
146        data = new char[length];
147        memcpy(data, dataString.string(), length);
148        drmInfo = new DrmInfo(drmInfoRequest->getInfoType(),
149            DrmBuffer(data, length), drmInfoRequest->getMimeType());
150    }
151    return drmInfo;
152}
153
154bool DrmPassthruPlugIn::onCanHandle(int uniqueId, const String8& path) {
155    LOGD("DrmPassthruPlugIn::canHandle: %s ", path.string());
156    String8 extension = path.getPathExtension();
157    extension.toLower();
158    return (String8(".passthru") == extension);
159}
160
161String8 DrmPassthruPlugIn::onGetOriginalMimeType(int uniqueId, const String8& path) {
162    LOGD("DrmPassthruPlugIn::onGetOriginalMimeType() : %d", uniqueId);
163    return String8("video/passthru");
164}
165
166int DrmPassthruPlugIn::onGetDrmObjectType(
167            int uniqueId, const String8& path, const String8& mimeType) {
168    LOGD("DrmPassthruPlugIn::onGetDrmObjectType() : %d", uniqueId);
169    return DrmObjectType::UNKNOWN;
170}
171
172int DrmPassthruPlugIn::onCheckRightsStatus(int uniqueId, const String8& path, int action) {
173    LOGD("DrmPassthruPlugIn::onCheckRightsStatus() : %d", uniqueId);
174    int rightsStatus = RightsStatus::RIGHTS_VALID;
175    return rightsStatus;
176}
177
178status_t DrmPassthruPlugIn::onConsumeRights(int uniqueId, DecryptHandle* decryptHandle,
179            int action, bool reserve) {
180    LOGD("DrmPassthruPlugIn::onConsumeRights() : %d", uniqueId);
181    return DRM_NO_ERROR;
182}
183
184status_t DrmPassthruPlugIn::onSetPlaybackStatus(int uniqueId, DecryptHandle* decryptHandle,
185            int playbackStatus, int position) {
186    LOGD("DrmPassthruPlugIn::onSetPlaybackStatus() : %d", uniqueId);
187    return DRM_NO_ERROR;
188}
189
190bool DrmPassthruPlugIn::onValidateAction(int uniqueId, const String8& path,
191            int action, const ActionDescription& description) {
192    LOGD("DrmPassthruPlugIn::onValidateAction() : %d", uniqueId);
193    return true;
194}
195
196status_t DrmPassthruPlugIn::onRemoveRights(int uniqueId, const String8& path) {
197    LOGD("DrmPassthruPlugIn::onRemoveRights() : %d", uniqueId);
198    return DRM_NO_ERROR;
199}
200
201status_t DrmPassthruPlugIn::onRemoveAllRights(int uniqueId) {
202    LOGD("DrmPassthruPlugIn::onRemoveAllRights() : %d", uniqueId);
203    return DRM_NO_ERROR;
204}
205
206status_t DrmPassthruPlugIn::onOpenConvertSession(int uniqueId, int convertId) {
207    LOGD("DrmPassthruPlugIn::onOpenConvertSession() : %d", uniqueId);
208    return DRM_NO_ERROR;
209}
210
211DrmConvertedStatus* DrmPassthruPlugIn::onConvertData(
212            int uniqueId, int convertId, const DrmBuffer* inputData) {
213    LOGD("DrmPassthruPlugIn::onConvertData() : %d", uniqueId);
214    DrmBuffer* convertedData = NULL;
215
216    if (NULL != inputData && 0 < inputData->length) {
217        int length = inputData->length;
218        char* data = NULL;
219        data = new char[length];
220        convertedData = new DrmBuffer(data, length);
221        memcpy(convertedData->data, inputData->data, length);
222    }
223    return new DrmConvertedStatus(DrmConvertedStatus::STATUS_OK, convertedData, 0 /*offset*/);
224}
225
226DrmConvertedStatus* DrmPassthruPlugIn::onCloseConvertSession(int uniqueId, int convertId) {
227    LOGD("DrmPassthruPlugIn::onCloseConvertSession() : %d", uniqueId);
228    return new DrmConvertedStatus(DrmConvertedStatus::STATUS_OK, NULL, 0 /*offset*/);
229}
230
231status_t DrmPassthruPlugIn::onOpenDecryptSession(
232            int uniqueId, DecryptHandle* decryptHandle, int fd, int offset, int length) {
233    LOGD("DrmPassthruPlugIn::onOpenDecryptSession() : %d", uniqueId);
234
235#ifdef ENABLE_PASSTHRU_DECRYPTION
236    decryptHandle->mimeType = String8("video/passthru");
237    decryptHandle->decryptApiType = DecryptApiType::ELEMENTARY_STREAM_BASED;
238    decryptHandle->status = DRM_NO_ERROR;
239    decryptHandle->decryptInfo = NULL;
240    return DRM_NO_ERROR;
241#endif
242
243    return DRM_ERROR_CANNOT_HANDLE;
244}
245
246status_t DrmPassthruPlugIn::onCloseDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
247    LOGD("DrmPassthruPlugIn::onCloseDecryptSession() : %d", uniqueId);
248    if (NULL != decryptHandle) {
249        if (NULL != decryptHandle->decryptInfo) {
250            delete decryptHandle->decryptInfo; decryptHandle->decryptInfo = NULL;
251        }
252        delete decryptHandle; decryptHandle = NULL;
253    }
254    return DRM_NO_ERROR;
255}
256
257status_t DrmPassthruPlugIn::onInitializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
258            int decryptUnitId, const DrmBuffer* headerInfo) {
259    LOGD("DrmPassthruPlugIn::onInitializeDecryptUnit() : %d", uniqueId);
260    return DRM_NO_ERROR;
261}
262
263status_t DrmPassthruPlugIn::onDecrypt(int uniqueId, DecryptHandle* decryptHandle,
264            int decryptUnitId, const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
265    LOGD("DrmPassthruPlugIn::onDecrypt() : %d", uniqueId);
266    /**
267     * As a workaround implementation passthru would copy the given
268     * encrypted buffer as it is to decrypted buffer. Note, decBuffer
269     * memory has to be allocated by the caller.
270     */
271    if (NULL != (*decBuffer) && 0 < (*decBuffer)->length) {
272        memcpy((*decBuffer)->data, encBuffer->data, encBuffer->length);
273        (*decBuffer)->length = encBuffer->length;
274    }
275    return DRM_NO_ERROR;
276}
277
278status_t DrmPassthruPlugIn::onFinalizeDecryptUnit(
279            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
280    LOGD("DrmPassthruPlugIn::onFinalizeDecryptUnit() : %d", uniqueId);
281    return DRM_NO_ERROR;
282}
283
284ssize_t DrmPassthruPlugIn::onPread(int uniqueId, DecryptHandle* decryptHandle,
285            void* buffer, ssize_t numBytes, off_t offset) {
286    LOGD("DrmPassthruPlugIn::onPread() : %d", uniqueId);
287    return 0;
288}
289
290