blob: 2e050935ba5b096034ddc2ecb36470724919c573 [file] [log] [blame]
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -07001/*
2**
3** Copyright 2013, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "IDrmClient"
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070020
Robert Shih61e1c762019-10-31 21:26:58 -070021#include <utils/Errors.h>
22#include <utils/Log.h>
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070023#include <utils/RefBase.h>
24#include <binder/IInterface.h>
25#include <binder/Parcel.h>
Robert Shih61e1c762019-10-31 21:26:58 -070026#include <hidl/HidlSupport.h>
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070027
28#include <media/IMediaPlayerClient.h>
Robert Shih61e1c762019-10-31 21:26:58 -070029#include <mediadrm/DrmUtils.h>
Jeff Tinker7d2c6e82018-02-16 16:14:59 -080030#include <mediadrm/IDrmClient.h>
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070031
Robert Shih61e1c762019-10-31 21:26:58 -070032#include <cstddef>
33#include <cstdint>
34#include <vector>
35
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070036namespace android {
37
38enum {
Robert Shih61e1c762019-10-31 21:26:58 -070039 SEND_EVENT = IBinder::FIRST_CALL_TRANSACTION,
40 SEND_EXPIRATION_UPDATE,
41 SEND_KEYS_CHANGE,
42 SEND_SESSION_LOST_STATE,
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070043};
44
Robert Shih61e1c762019-10-31 21:26:58 -070045namespace {
46
47hardware::hidl_vec<uint8_t> ReadByteArray(const Parcel &obj, status_t *err)
48{
49 int32_t len = obj.readInt32();
50 hardware::hidl_vec<uint8_t> ret;
51 if (len < 0) {
52 ALOGE("Invalid array len");
53 *err = BAD_VALUE;
54 return ret;
55 }
56 ret.resize(static_cast<size_t>(len));
57 *err = obj.read(ret.data(), ret.size());
58 return ret;
59}
60
61}
62
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070063class BpDrmClient: public BpInterface<IDrmClient>
64{
Robert Shih61e1c762019-10-31 21:26:58 -070065 template <typename F>
66 void notify(uint32_t code, F fillParcel) {
67 Parcel obj, reply;
68 obj.writeInterfaceToken(IDrmClient::getInterfaceDescriptor());
69 fillParcel(obj);
70 remote()->transact(code, obj, &reply, IBinder::FLAG_ONEWAY);
71 }
72
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070073public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070074 explicit BpDrmClient(const sp<IBinder>& impl)
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070075 : BpInterface<IDrmClient>(impl)
76 {
77 }
78
Robert Shih61e1c762019-10-31 21:26:58 -070079 virtual void sendEvent(
80 DrmPlugin::EventType eventType,
81 const hardware::hidl_vec<uint8_t> &sessionId,
82 const hardware::hidl_vec<uint8_t> &data)
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -070083 {
Robert Shih61e1c762019-10-31 21:26:58 -070084 auto fillParcel = [&] (Parcel &p) {
85 DrmUtils::WriteEventToParcel(p, eventType, sessionId, data);
86 };
87 notify(SEND_EVENT, fillParcel);
88 }
89
90 virtual void sendExpirationUpdate(
91 const hardware::hidl_vec<uint8_t> &sessionId,
92 int64_t expiryTimeInMS)
93 {
94 auto fillParcel = [&] (Parcel &p) {
95 DrmUtils::WriteExpirationUpdateToParcel(p, sessionId, expiryTimeInMS);
96 };
97 notify(SEND_EXPIRATION_UPDATE, fillParcel);
98 }
99
100 virtual void sendKeysChange(
101 const hardware::hidl_vec<uint8_t> &sessionId,
102 const std::vector<DrmKeyStatus> &keyStatusList,
103 bool hasNewUsableKey)
104 {
105 auto fillParcel = [&] (Parcel &p) {
106 DrmUtils::WriteKeysChange(p, sessionId, keyStatusList, hasNewUsableKey);
107 };
108 notify(SEND_KEYS_CHANGE, fillParcel);
109 }
110
111 virtual void sendSessionLostState(
112 const hardware::hidl_vec<uint8_t> &sessionId)
113 {
114 auto fillParcel = [&] (Parcel &p) {
115 DrmUtils::WriteByteArray(p, sessionId);
116 };
117 notify(SEND_SESSION_LOST_STATE, fillParcel);
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -0700118 }
119};
120
121IMPLEMENT_META_INTERFACE(DrmClient, "android.media.IDrmClient");
122
123// ----------------------------------------------------------------------
124
125status_t BnDrmClient::onTransact(
Robert Shih61e1c762019-10-31 21:26:58 -0700126 uint32_t code, const Parcel& obj, Parcel* reply, uint32_t flags)
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -0700127{
Robert Shih61e1c762019-10-31 21:26:58 -0700128 CHECK_INTERFACE(IDrmClient, obj, reply);
129 status_t err = NO_ERROR;
130 hardware::hidl_vec<uint8_t> sessionId(ReadByteArray(obj, &err));
131 if (err != NO_ERROR) {
132 ALOGE("Failed to read session id, error=%d", err);
133 return err;
134 }
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -0700135
Robert Shih61e1c762019-10-31 21:26:58 -0700136 switch (code) {
137 case SEND_EVENT: {
138 hardware::hidl_vec<uint8_t> data(ReadByteArray(obj, &err));
139 int eventType = obj.readInt32();
140 if (err == NO_ERROR) {
141 sendEvent(static_cast<DrmPlugin::EventType>(eventType), sessionId, data);
142 }
143 return err;
144 } break;
145 case SEND_EXPIRATION_UPDATE: {
146 int64_t expiryTimeInMS = obj.readInt64();
147 sendExpirationUpdate(sessionId, expiryTimeInMS);
148 return NO_ERROR;
149 } break;
150 case SEND_KEYS_CHANGE: {
151 // ...
152 int32_t n = obj.readInt32();
153 if (n < 0) {
154 return BAD_VALUE;
155 }
156 std::vector<DrmKeyStatus> keyStatusList;
157 for (int32_t i = 0; i < n; ++i) {
158 hardware::hidl_vec<uint8_t> keyId(ReadByteArray(obj, &err));
159 if (err != NO_ERROR) {
160 return err;
161 }
162 int32_t type = obj.readInt32();
163 if (type < 0) {
164 return BAD_VALUE;
165 }
166 keyStatusList.push_back({static_cast<uint32_t>(type), keyId});
167 }
168 int32_t hasNewUsableKey = obj.readInt32();
169 sendKeysChange(sessionId, keyStatusList, hasNewUsableKey);
170 return NO_ERROR;
171 } break;
172 case SEND_SESSION_LOST_STATE: {
173 sendSessionLostState(sessionId);
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -0700174 return NO_ERROR;
175 } break;
176 default:
Robert Shih61e1c762019-10-31 21:26:58 -0700177 return BBinder::onTransact(code, obj, reply, flags);
Jeff Tinkerc0d5f1f2013-04-02 13:08:05 -0700178 }
179}
180
Glenn Kasten40bc9062015-03-20 09:09:33 -0700181} // namespace android