blob: dc0793af2d1740710d186f34b2fa4e2fbb0815db [file] [log] [blame]
Jeff Tinker8a0c80f2013-02-08 10:20:44 -08001/*
2 * Copyright 2013, 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#ifndef _ANDROID_MEDIA_DRM_H_
18#define _ANDROID_MEDIA_DRM_H_
19
20#include "jni.h"
21
22#include <media/stagefright/foundation/ABase.h>
Marco Nelissena2eedd5d2019-09-27 11:19:36 -070023#include <mediadrm/IDrm.h>
Robert Shih3a523902019-08-15 14:48:11 -070024#include <mediadrm/IDrmClient.h>
25#include <hidl/HidlSupport.h>
Jeff Tinker8a0c80f2013-02-08 10:20:44 -080026#include <utils/Errors.h>
27#include <utils/RefBase.h>
28
Robert Shih9d4e2d42019-11-08 13:51:49 -080029namespace {
30
Robert Shih620f4a62021-02-15 03:28:42 -080031enum {
32 // TODO(b/180483929): use reverse jni e.g. android_media_MediaDrm_native_init
33 // KEEP IN SYNC with MediaDrm$ErrorCodes in MediaDrm.java!
34 JERROR_DRM_UNKNOWN = 0,
35 JERROR_DRM_NO_LICENSE = 1,
36 JERROR_DRM_LICENSE_EXPIRED = 2,
37 JERROR_DRM_RESOURCE_BUSY = 3,
38 JERROR_DRM_INSUFFICIENT_OUTPUT_PROTECTION = 4,
39 JERROR_DRM_SESSION_NOT_OPENED = 5,
40 JERROR_DRM_CANNOT_HANDLE = 6,
41 JERROR_DRM_INSUFFICIENT_SECURITY = 7,
42 JERROR_DRM_FRAME_TOO_LARGE = 8,
43 JERROR_DRM_SESSION_LOST_STATE = 9,
44 JERROR_DRM_CERTIFICATE_MALFORMED = 10,
45 JERROR_DRM_CERTIFICATE_MISSING = 11,
46 JERROR_DRM_CRYPTO_LIBRARY = 12,
47 JERROR_DRM_GENERIC_OEM = 13,
48 JERROR_DRM_GENERIC_PLUGIN = 14,
49 JERROR_DRM_INIT_DATA = 15,
50 JERROR_DRM_KEY_NOT_LOADED = 16,
51 JERROR_DRM_LICENSE_PARSE = 17,
52 JERROR_DRM_LICENSE_POLICY = 18,
53 JERROR_DRM_LICENSE_RELEASE = 19,
54 JERROR_DRM_LICENSE_REQUEST_REJECTED = 20,
55 JERROR_DRM_LICENSE_RESTORE = 21,
56 JERROR_DRM_LICENSE_STATE = 22,
57 JERROR_DRM_MEDIA_FRAMEWORK = 23,
58 JERROR_DRM_PROVISIONING_CERTIFICATE = 24,
59 JERROR_DRM_PROVISIONING_CONFIG = 25,
60 JERROR_DRM_PROVISIONING_PARSE = 26,
61 JERROR_DRM_PROVISIONING_RETRY = 27,
62 JERROR_DRM_RESOURCE_CONTENTION = 28,
63 JERROR_DRM_SECURE_STOP_RELEASE = 29,
64 JERROR_DRM_STORAGE_READ = 30,
65 JERROR_DRM_STORAGE_WRITE = 31,
66 JERROR_DRM_ZERO_SUBSAMPLES = 32,
67};
68
Robert Shih9d4e2d42019-11-08 13:51:49 -080069struct ListenerArgs {
70 jbyteArray jSessionId;
71 jbyteArray jData;
72 jlong jExpirationTime;
73 jobject jKeyStatusList;
74 jboolean jHasNewUsableKey;
75};
76
77}
78
Jeff Tinker8a0c80f2013-02-08 10:20:44 -080079namespace android {
80
Jeff Tinker54cfbd62013-04-02 13:14:59 -070081class DrmListener: virtual public RefBase
82{
83public:
84 virtual void notify(DrmPlugin::EventType eventType, int extra,
Robert Shih9d4e2d42019-11-08 13:51:49 -080085 const ListenerArgs *args) = 0;
Jeff Tinker54cfbd62013-04-02 13:14:59 -070086};
87
Robert Shih0d057e52019-11-12 12:46:48 -080088struct JDrm : public IDrmClient {
Jeff Tinker5de2e902019-01-25 23:09:36 -080089 static status_t IsCryptoSchemeSupported(const uint8_t uuid[16],
90 const String8 &mimeType,
91 DrmPlugin::SecurityLevel level,
92 bool *isSupported);
Jeff Tinker8a0c80f2013-02-08 10:20:44 -080093
Edwin Wong4d1d84e2017-01-04 09:37:49 -080094 JDrm(JNIEnv *env, jobject thiz, const uint8_t uuid[16], const String8 &appPackageName);
Jeff Tinker8a0c80f2013-02-08 10:20:44 -080095
96 status_t initCheck() const;
Jeff Tinker8a0c80f2013-02-08 10:20:44 -080097 sp<IDrm> getDrm() { return mDrm; }
98
Robert Shih3a523902019-08-15 14:48:11 -070099 void sendEvent(
100 DrmPlugin::EventType eventType,
101 const hardware::hidl_vec<uint8_t> &sessionId,
102 const hardware::hidl_vec<uint8_t> &data) override;
103
104 void sendExpirationUpdate(
105 const hardware::hidl_vec<uint8_t> &sessionId,
106 int64_t expiryTimeInMS) override;
107
108 void sendKeysChange(
109 const hardware::hidl_vec<uint8_t> &sessionId,
110 const std::vector<DrmKeyStatus> &keyStatusList,
111 bool hasNewUsableKey) override;
112
113 void sendSessionLostState(
114 const hardware::hidl_vec<uint8_t> &sessionId) override;
115
Jeff Tinker54cfbd62013-04-02 13:14:59 -0700116 status_t setListener(const sp<DrmListener>& listener);
117
Jeff Tinker600071c2014-04-11 16:11:15 -0700118 void disconnect();
119
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800120protected:
121 virtual ~JDrm();
122
123private:
124 jweak mObject;
125 sp<IDrm> mDrm;
126
Jeff Tinker54cfbd62013-04-02 13:14:59 -0700127 sp<DrmListener> mListener;
128 Mutex mNotifyLock;
129 Mutex mLock;
130
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800131 static sp<IDrm> MakeDrm();
Edwin Wong4d1d84e2017-01-04 09:37:49 -0800132 static sp<IDrm> MakeDrm(const uint8_t uuid[16], const String8 &appPackageName);
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800133
Robert Shih9d4e2d42019-11-08 13:51:49 -0800134 void notify(DrmPlugin::EventType, int extra, const ListenerArgs *args);
Robert Shih3a523902019-08-15 14:48:11 -0700135
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800136 DISALLOW_EVIL_CONSTRUCTORS(JDrm);
137};
138
Robert Shih620f4a62021-02-15 03:28:42 -0800139jint MediaErrorToJavaError(status_t err);
140
Jeff Tinker8a0c80f2013-02-08 10:20:44 -0800141} // namespace android
142
143#endif // _ANDROID_MEDIA_DRM_H_