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 | |
| 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 | |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 38 | static media_status_t translate_error(status_t err) { |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 39 | if (err == OK) { |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 40 | return AMEDIA_OK; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 41 | } |
| 42 | ALOGE("sf error code: %d", err); |
Marco Nelissen | e419d7c | 2014-05-15 14:17:25 -0700 | [diff] [blame] | 43 | return AMEDIA_ERROR_UNKNOWN; |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 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 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 77 | EXPORT |
Marco Nelissen | 829e097 | 2014-05-13 16:22:19 -0700 | [diff] [blame] | 78 | bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) { |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 79 | sp<ICrypto> crypto = makeCrypto(); |
| 80 | if (crypto == NULL) { |
| 81 | return false; |
| 82 | } |
| 83 | return crypto->isCryptoSchemeSupported(uuid); |
| 84 | } |
| 85 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 86 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 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 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 95 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 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 | |
Marco Nelissen | 3425fd5 | 2014-05-14 11:12:46 -0700 | [diff] [blame] | 113 | EXPORT |
Marco Nelissen | 050eb32 | 2014-05-09 15:10:23 -0700 | [diff] [blame] | 114 | void AMediaCrypto_delete(AMediaCrypto* crypto) { |
| 115 | delete crypto; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | } // extern "C" |
| 121 | |