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 "NdkMediaCrypto.h"
22 #include "NdkMediaCodec.h"
23 #include "NdkMediaFormatPriv.h"
24 
25 
26 #include <utils/Log.h>
27 #include <utils/StrongPointer.h>
28 #include <binder/IServiceManager.h>
29 #include <media/ICrypto.h>
30 #include <media/IMediaPlayerService.h>
31 #include <android_runtime/AndroidRuntime.h>
32 #include <android_util_Binder.h>
33 
34 #include <jni.h>
35 
36 using namespace android;
37 
translate_error(status_t err)38 static media_status_t translate_error(status_t err) {
39     if (err == OK) {
40         return AMEDIA_OK;
41     }
42     ALOGE("sf error code: %d", err);
43     return AMEDIA_ERROR_UNKNOWN;
44 }
45 
46 
makeCrypto()47 static sp<ICrypto> makeCrypto() {
48     sp<IServiceManager> sm = defaultServiceManager();
49 
50     sp<IBinder> binder =
51         sm->getService(String16("media.player"));
52 
53     sp<IMediaPlayerService> service =
54         interface_cast<IMediaPlayerService>(binder);
55 
56     if (service == NULL) {
57         return NULL;
58     }
59 
60     sp<ICrypto> crypto = service->makeCrypto();
61 
62     if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) {
63         return NULL;
64     }
65 
66     return crypto;
67 }
68 
69 struct AMediaCrypto {
70     sp<ICrypto> mCrypto;
71 };
72 
73 
74 extern "C" {
75 
76 
77 EXPORT
AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid)78 bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) {
79     sp<ICrypto> crypto = makeCrypto();
80     if (crypto == NULL) {
81         return false;
82     }
83     return crypto->isCryptoSchemeSupported(uuid);
84 }
85 
86 EXPORT
AMediaCrypto_requiresSecureDecoderComponent(const char * mime)87 bool AMediaCrypto_requiresSecureDecoderComponent(const char *mime) {
88     sp<ICrypto> crypto = makeCrypto();
89     if (crypto == NULL) {
90         return false;
91     }
92     return crypto->requiresSecureDecoderComponent(mime);
93 }
94 
95 EXPORT
AMediaCrypto_new(const AMediaUUID uuid,const void * data,size_t datasize)96 AMediaCrypto* AMediaCrypto_new(const AMediaUUID uuid, const void *data, size_t datasize) {
97 
98     sp<ICrypto> tmp = makeCrypto();
99     if (tmp == NULL) {
100         return NULL;
101     }
102 
103     if (tmp->createPlugin(uuid, data, datasize) != 0) {
104         return NULL;
105     }
106 
107     AMediaCrypto *crypto = new AMediaCrypto();
108     crypto->mCrypto = tmp;
109 
110     return crypto;
111 }
112 
113 EXPORT
AMediaCrypto_delete(AMediaCrypto * crypto)114 void AMediaCrypto_delete(AMediaCrypto* crypto) {
115     delete crypto;
116 }
117 
118 
119 
120 } // extern "C"
121 
122