| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 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 |  | 
| Marco Nelissen | c7a11b2 | 2014-05-30 10:13:25 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 18 | #define LOG_TAG "NdkMediaCrypto" | 
 | 19 |  | 
 | 20 |  | 
| Colin Cross | 7e8d4ba | 2017-05-04 16:17:42 -0700 | [diff] [blame] | 21 | #include <media/NdkMediaCrypto.h> | 
 | 22 | #include <media/NdkMediaCodec.h> | 
| Marco Nelissen | 98603d8 | 2018-07-17 11:06:55 -0700 | [diff] [blame^] | 23 | #include <media/NdkMediaFormatPriv.h> | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 24 |  | 
 | 25 |  | 
| Jeff Tinker | a69729d | 2016-02-12 08:47:00 -0800 | [diff] [blame] | 26 | #include <cutils/properties.h> | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> | 
 | 28 | #include <utils/StrongPointer.h> | 
 | 29 | #include <binder/IServiceManager.h> | 
 | 30 | #include <media/ICrypto.h> | 
| Jeff Tinker | a69729d | 2016-02-12 08:47:00 -0800 | [diff] [blame] | 31 | #include <media/IMediaDrmService.h> | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 32 | #include <android_runtime/AndroidRuntime.h> | 
 | 33 | #include <android_util_Binder.h> | 
 | 34 |  | 
 | 35 | #include <jni.h> | 
 | 36 |  | 
 | 37 | using namespace android; | 
 | 38 |  | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 39 | static sp<ICrypto> makeCrypto() { | 
 | 40 |     sp<IServiceManager> sm = defaultServiceManager(); | 
| Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 41 |     sp<IBinder> binder = sm->getService(String16("media.drm")); | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 42 |  | 
| Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 43 |     sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder); | 
 | 44 |     if (service == NULL) { | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 45 |         return NULL; | 
 | 46 |     } | 
 | 47 |  | 
| Jeff Tinker | 3003807 | 2016-04-25 13:41:35 -0700 | [diff] [blame] | 48 |     sp<ICrypto> crypto = service->makeCrypto(); | 
 | 49 |     if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) { | 
 | 50 |         return NULL; | 
 | 51 |     } | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 52 |     return crypto; | 
 | 53 | } | 
 | 54 |  | 
 | 55 | struct AMediaCrypto { | 
 | 56 |     sp<ICrypto> mCrypto; | 
 | 57 | }; | 
 | 58 |  | 
 | 59 |  | 
 | 60 | extern "C" { | 
 | 61 |  | 
 | 62 |  | 
| Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 63 | EXPORT | 
| Marco Nelissen | 829e097 | 2014-05-13 16:22:19 -0700 | [diff] [blame] | 64 | bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) { | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 65 |     sp<ICrypto> crypto = makeCrypto(); | 
 | 66 |     if (crypto == NULL) { | 
 | 67 |         return false; | 
 | 68 |     } | 
 | 69 |     return crypto->isCryptoSchemeSupported(uuid); | 
 | 70 | } | 
 | 71 |  | 
| Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 72 | EXPORT | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 73 | bool AMediaCrypto_requiresSecureDecoderComponent(const char *mime) { | 
 | 74 |     sp<ICrypto> crypto = makeCrypto(); | 
 | 75 |     if (crypto == NULL) { | 
 | 76 |         return false; | 
 | 77 |     } | 
 | 78 |     return crypto->requiresSecureDecoderComponent(mime); | 
 | 79 | } | 
 | 80 |  | 
| Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 81 | EXPORT | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 82 | AMediaCrypto* AMediaCrypto_new(const AMediaUUID uuid, const void *data, size_t datasize) { | 
 | 83 |  | 
 | 84 |     sp<ICrypto> tmp = makeCrypto(); | 
 | 85 |     if (tmp == NULL) { | 
 | 86 |         return NULL; | 
 | 87 |     } | 
 | 88 |  | 
 | 89 |     if (tmp->createPlugin(uuid, data, datasize) != 0) { | 
 | 90 |         return NULL; | 
 | 91 |     } | 
 | 92 |  | 
 | 93 |     AMediaCrypto *crypto = new AMediaCrypto(); | 
 | 94 |     crypto->mCrypto = tmp; | 
 | 95 |  | 
 | 96 |     return crypto; | 
 | 97 | } | 
 | 98 |  | 
| Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 99 | EXPORT | 
| Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 100 | void AMediaCrypto_delete(AMediaCrypto* crypto) { | 
 | 101 |     delete crypto; | 
 | 102 | } | 
 | 103 |  | 
 | 104 |  | 
 | 105 |  | 
 | 106 | } // extern "C" | 
 | 107 |  |