Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 20 | |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 21 | #include <utils/Errors.h> |
| 22 | #include <utils/Log.h> |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 23 | #include <utils/RefBase.h> |
| 24 | #include <binder/IInterface.h> |
| 25 | #include <binder/Parcel.h> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 26 | #include <hidl/HidlSupport.h> |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 27 | |
| 28 | #include <media/IMediaPlayerClient.h> |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 29 | #include <mediadrm/DrmUtils.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 30 | #include <mediadrm/IDrmClient.h> |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 31 | |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 32 | #include <cstddef> |
| 33 | #include <cstdint> |
| 34 | #include <vector> |
| 35 | |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | enum { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 39 | SEND_EVENT = IBinder::FIRST_CALL_TRANSACTION, |
| 40 | SEND_EXPIRATION_UPDATE, |
| 41 | SEND_KEYS_CHANGE, |
| 42 | SEND_SESSION_LOST_STATE, |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | hardware::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 Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 63 | class BpDrmClient: public BpInterface<IDrmClient> |
| 64 | { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 65 | 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 Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 73 | public: |
Chih-Hung Hsieh | 090ef60 | 2016-04-27 10:39:54 -0700 | [diff] [blame] | 74 | explicit BpDrmClient(const sp<IBinder>& impl) |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 75 | : BpInterface<IDrmClient>(impl) |
| 76 | { |
| 77 | } |
| 78 | |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 79 | virtual void sendEvent( |
| 80 | DrmPlugin::EventType eventType, |
| 81 | const hardware::hidl_vec<uint8_t> &sessionId, |
| 82 | const hardware::hidl_vec<uint8_t> &data) |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 83 | { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 84 | 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 Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | |
| 121 | IMPLEMENT_META_INTERFACE(DrmClient, "android.media.IDrmClient"); |
| 122 | |
| 123 | // ---------------------------------------------------------------------- |
| 124 | |
| 125 | status_t BnDrmClient::onTransact( |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 126 | uint32_t code, const Parcel& obj, Parcel* reply, uint32_t flags) |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 127 | { |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 128 | 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 Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 135 | |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 136 | 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 Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 174 | return NO_ERROR; |
| 175 | } break; |
| 176 | default: |
Robert Shih | 61e1c76 | 2019-10-31 21:26:58 -0700 | [diff] [blame] | 177 | return BBinder::onTransact(code, obj, reply, flags); |
Jeff Tinker | c0d5f1f | 2013-04-02 13:08:05 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 181 | } // namespace android |