blob: af8ffea1afc2fbb2dd28ead33ab13f09b28c16c0 [file] [log] [blame]
Marco Nelissen050eb322014-05-09 15:10:23 -07001/*
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 Nelissenc7a11b22014-05-30 10:13:25 -070017//#define LOG_NDEBUG 0
Marco Nelissen050eb322014-05-09 15:10:23 -070018#define LOG_TAG "NdkMediaCrypto"
19
20
21#include "NdkMediaCrypto.h"
22#include "NdkMediaCodec.h"
23#include "NdkMediaFormatPriv.h"
24
25
Jeff Tinkera69729d2016-02-12 08:47:00 -080026#include <cutils/properties.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070027#include <utils/Log.h>
28#include <utils/StrongPointer.h>
29#include <binder/IServiceManager.h>
30#include <media/ICrypto.h>
Jeff Tinkera69729d2016-02-12 08:47:00 -080031#include <media/IMediaDrmService.h>
Marco Nelissen050eb322014-05-09 15:10:23 -070032#include <media/IMediaPlayerService.h>
33#include <android_runtime/AndroidRuntime.h>
34#include <android_util_Binder.h>
35
36#include <jni.h>
37
38using namespace android;
39
Marco Nelissene419d7c2014-05-15 14:17:25 -070040static media_status_t translate_error(status_t err) {
Marco Nelissen050eb322014-05-09 15:10:23 -070041 if (err == OK) {
Marco Nelissene419d7c2014-05-15 14:17:25 -070042 return AMEDIA_OK;
Marco Nelissen050eb322014-05-09 15:10:23 -070043 }
44 ALOGE("sf error code: %d", err);
Marco Nelissene419d7c2014-05-15 14:17:25 -070045 return AMEDIA_ERROR_UNKNOWN;
Marco Nelissen050eb322014-05-09 15:10:23 -070046}
47
48
49static sp<ICrypto> makeCrypto() {
50 sp<IServiceManager> sm = defaultServiceManager();
Jeff Tinkera69729d2016-02-12 08:47:00 -080051 sp<ICrypto> crypto;
Marco Nelissen050eb322014-05-09 15:10:23 -070052
Jeff Tinkera69729d2016-02-12 08:47:00 -080053 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 Nelissen050eb322014-05-09 15:10:23 -070073 }
74
Marco Nelissen050eb322014-05-09 15:10:23 -070075 if (crypto == NULL || (crypto->initCheck() != OK && crypto->initCheck() != NO_INIT)) {
76 return NULL;
77 }
78
79 return crypto;
80}
81
82struct AMediaCrypto {
83 sp<ICrypto> mCrypto;
84};
85
86
87extern "C" {
88
89
Marco Nelissen3425fd52014-05-14 11:12:46 -070090EXPORT
Marco Nelissen829e0972014-05-13 16:22:19 -070091bool AMediaCrypto_isCryptoSchemeSupported(const AMediaUUID uuid) {
Marco Nelissen050eb322014-05-09 15:10:23 -070092 sp<ICrypto> crypto = makeCrypto();
93 if (crypto == NULL) {
94 return false;
95 }
96 return crypto->isCryptoSchemeSupported(uuid);
97}
98
Marco Nelissen3425fd52014-05-14 11:12:46 -070099EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700100bool 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 Nelissen3425fd52014-05-14 11:12:46 -0700108EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700109AMediaCrypto* 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 Nelissen3425fd52014-05-14 11:12:46 -0700126EXPORT
Marco Nelissen050eb322014-05-09 15:10:23 -0700127void AMediaCrypto_delete(AMediaCrypto* crypto) {
128 delete crypto;
129}
130
131
132
133} // extern "C"
134