1 /*
2  * Copyright (C) 2014 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 "NdkMediaCrypto"
19 
20 
21 #include <media/NdkMediaCrypto.h>
22 #include <media/NdkMediaCodec.h>
23 #include <media/NdkMediaFormatPriv.h>
24 
25 
26 #include <cutils/properties.h>
27 #include <utils/Log.h>
28 #include <utils/StrongPointer.h>
29 #include <binder/IServiceManager.h>
30 #include <mediadrm/ICrypto.h>
31 #include <mediadrm/IMediaDrmService.h>
32 #include <android_util_Binder.h>
33 
34 #include <jni.h>
35 
36 using namespace android;
37 
makeCrypto()38 static sp<ICrypto> makeCrypto() {
39     sp<IServiceManager> sm = defaultServiceManager();
40     sp<IBinder> binder = sm->getService(String16("media.drm"));
41 
42     sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder);
43     if (service == NULL) {
44         return NULL;
45     }
46 
47     sp<ICrypto> crypto = service->makeCrypto();
48     if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) {
49         return NULL;
50     }
51     return crypto;
52 }
53 
54 struct AMediaCrypto {
55     sp<ICrypto> mCrypto;
56 };
57 
58 
59 extern "C" {
60 
61 
62 EXPORT
AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid)63 bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) {
64     sp<ICrypto> crypto = makeCrypto();
65     if (crypto == NULL) {
66         return false;
67     }
68     return crypto->isCryptoSchemeSupported(uuid);
69 }
70 
71 EXPORT
AMediaCrypto_requiresSecureDecoderComponent(const char * mime)72 bool AMediaCrypto_requiresSecureDecoderComponent(const char *mime) {
73     sp<ICrypto> crypto = makeCrypto();
74     if (crypto == NULL) {
75         return false;
76     }
77     return crypto->requiresSecureDecoderComponent(mime);
78 }
79 
80 EXPORT
AMediaCrypto_new(const AMediaUUID uuid,const void * data,size_t datasize)81 AMediaCrypto* AMediaCrypto_new(const AMediaUUID uuid, const void *data, size_t datasize) {
82 
83     sp<ICrypto> tmp = makeCrypto();
84     if (tmp == NULL) {
85         return NULL;
86     }
87 
88     if (tmp->createPlugin(uuid, data, datasize) != 0) {
89         return NULL;
90     }
91 
92     AMediaCrypto *crypto = new AMediaCrypto();
93     crypto->mCrypto = tmp;
94 
95     return crypto;
96 }
97 
98 EXPORT
AMediaCrypto_delete(AMediaCrypto * crypto)99 void AMediaCrypto_delete(AMediaCrypto* crypto) {
100     delete crypto;
101 }
102 
103 
104 
105 } // extern "C"
106 
107