NdkMediaCrypto.cpp revision 829e097f832b4c4c41733f9b77121888204d993e
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 36using namespace android; 37 38static int translate_error(status_t err) { 39 if (err == OK) { 40 return OK; 41 } 42 ALOGE("sf error code: %d", err); 43 return -1000; 44} 45 46 47static 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 69struct AMediaCrypto { 70 sp<ICrypto> mCrypto; 71}; 72 73 74extern "C" { 75 76 77bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) { 78 sp<ICrypto> crypto = makeCrypto(); 79 if (crypto == NULL) { 80 return false; 81 } 82 return crypto->isCryptoSchemeSupported(uuid); 83} 84 85bool AMediaCrypto_requiresSecureDecoderComponent(const char *mime) { 86 sp<ICrypto> crypto = makeCrypto(); 87 if (crypto == NULL) { 88 return false; 89 } 90 return crypto->requiresSecureDecoderComponent(mime); 91} 92 93AMediaCrypto* AMediaCrypto_new(const AMediaUUID uuid, const void *data, size_t datasize) { 94 95 sp<ICrypto> tmp = makeCrypto(); 96 if (tmp == NULL) { 97 return NULL; 98 } 99 100 if (tmp->createPlugin(uuid, data, datasize) != 0) { 101 return NULL; 102 } 103 104 AMediaCrypto *crypto = new AMediaCrypto(); 105 crypto->mCrypto = tmp; 106 107 return crypto; 108} 109 110void AMediaCrypto_delete(AMediaCrypto* crypto) { 111 delete crypto; 112} 113 114 115 116} // extern "C" 117 118