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 | |
| 21 | #include "NdkMediaCrypto.h" |
| 22 | #include "NdkMediaCodec.h" |
| 23 | #include "NdkMediaFormatPriv.h" |
| 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 <media/IMediaPlayerService.h> |
| 33 | #include <android_runtime/AndroidRuntime.h> |
| 34 | #include <android_util_Binder.h> |
| 35 | |
| 36 | #include <jni.h> |
| 37 | |
| 38 | using namespace android; |
| 39 | |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 40 | static media_status_t translate_error(status_t err) { |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 41 | if (err == OK) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 42 | return AMEDIA_OK; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 43 | } |
| 44 | ALOGE("sf error code: %d", err); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 45 | return AMEDIA_ERROR_UNKNOWN; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | static sp<ICrypto> makeCrypto() { |
| 50 | sp<IServiceManager> sm = defaultServiceManager(); |
Jeff Tinker | a69729d | 2016-02-12 08:47:00 -0800 | [diff] [blame] | 51 | sp<ICrypto> crypto; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 52 | |
Jeff Tinker | a69729d | 2016-02-12 08:47:00 -0800 | [diff] [blame] | 53 | char value[PROPERTY_VALUE_MAX]; |
| 54 | if (property_get("media.mediadrmservice.enable", value, NULL) |
| 55 | && (!strcmp("1", value) || !strcasecmp("true", value))) { |
| 56 | sp<IBinder> binder = |
| 57 | sm->getService(String16("media.drm")); |
| 58 | sp<IMediaDrmService> service = |
| 59 | interface_cast<IMediaDrmService>(binder); |
| 60 | if (service == NULL) { |
| 61 | return NULL; |
| 62 | } |
| 63 | crypto = service->makeCrypto(); |
| 64 | } else { |
| 65 | sp<IBinder> binder = |
| 66 | sm->getService(String16("media.player")); |
| 67 | sp<IMediaPlayerService> service = |
| 68 | interface_cast<IMediaPlayerService>(binder); |
| 69 | if (service == NULL) { |
| 70 | return NULL; |
| 71 | } |
| 72 | crypto = service->makeCrypto(); |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 75 | if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | return crypto; |
| 80 | } |
| 81 | |
| 82 | struct AMediaCrypto { |
| 83 | sp<ICrypto> mCrypto; |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | extern "C" { |
| 88 | |
| 89 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 90 | EXPORT |
Marco Nelissen | 829e097 | 2014-05-13 16:22:19 -0700 | [diff] [blame] | 91 | bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) { |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 92 | sp<ICrypto> crypto = makeCrypto(); |
| 93 | if (crypto == NULL) { |
| 94 | return false; |
| 95 | } |
| 96 | return crypto->isCryptoSchemeSupported(uuid); |
| 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 | bool AMediaCrypto_requiresSecureDecoderComponent(const char *mime) { |
| 101 | sp<ICrypto> crypto = makeCrypto(); |
| 102 | if (crypto == NULL) { |
| 103 | return false; |
| 104 | } |
| 105 | return crypto->requiresSecureDecoderComponent(mime); |
| 106 | } |
| 107 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 108 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 109 | AMediaCrypto* AMediaCrypto_new(const AMediaUUID uuid, const void *data, size_t datasize) { |
| 110 | |
| 111 | sp<ICrypto> tmp = makeCrypto(); |
| 112 | if (tmp == NULL) { |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | if (tmp->createPlugin(uuid, data, datasize) != 0) { |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | AMediaCrypto *crypto = new AMediaCrypto(); |
| 121 | crypto->mCrypto = tmp; |
| 122 | |
| 123 | return crypto; |
| 124 | } |
| 125 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 126 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 127 | void AMediaCrypto_delete(AMediaCrypto* crypto) { |
| 128 | delete crypto; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | } // extern "C" |
| 134 | |