Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | #include "DescramblerTests.h" |
| 18 | |
| 19 | using namespace std; |
| 20 | |
| 21 | AssertionResult DescramblerTests::createCasPlugin(int32_t caSystemId) { |
Yixiao Luo | e8730a7 | 2023-02-01 18:39:51 -0800 | [diff] [blame] | 22 | mCasListener = ::ndk::SharedRefBase::make<MediaCasListener>(); |
| 23 | |
| 24 | if (mMediaCasServiceAidl != nullptr) { |
| 25 | bool rst = false; |
| 26 | ScopedAStatus status = mMediaCasServiceAidl->isSystemIdSupported(caSystemId, &rst); |
| 27 | if (!status.isOk() || !rst) { |
| 28 | ALOGW("[vts] Failed to check isSystemIdSupported for AIDL service."); |
| 29 | return failure(); |
| 30 | } |
| 31 | status = mMediaCasServiceAidl->createPlugin(caSystemId, mCasListener, &mCasAidl); |
| 32 | if (!status.isOk()) { |
| 33 | ALOGW("[vts] Failed to createPlugin for AIDL service."); |
| 34 | return failure(); |
| 35 | } |
| 36 | } else { |
| 37 | auto status = mMediaCasServiceHidl->isSystemIdSupported(caSystemId); |
| 38 | if (!status.isOk() || !status) { |
| 39 | ALOGW("[vts] Failed to check isSystemIdSupported for HIDL service."); |
| 40 | return failure(); |
| 41 | } |
| 42 | auto pluginStatus = mMediaCasServiceHidl->createPluginExt( |
| 43 | caSystemId, sp<ICasListenerHidl>(mCasListener.get())); |
| 44 | if (!pluginStatus.isOk()) { |
| 45 | ALOGW("[vts] Failed to createPluginExt for HIDL service."); |
| 46 | return failure(); |
| 47 | } |
| 48 | mCasHidl = ICasHidl::castFrom(pluginStatus); |
| 49 | if (mCasHidl == nullptr) { |
| 50 | ALOGW("[vts] Failed to get ICas for HIDL service."); |
| 51 | return failure(); |
| 52 | } |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 55 | return success(); |
| 56 | } |
| 57 | |
| 58 | AssertionResult DescramblerTests::openCasSession(vector<uint8_t>& sessionId, |
Yixiao Luo | e8730a7 | 2023-02-01 18:39:51 -0800 | [diff] [blame] | 59 | vector<uint8_t>& pvtData) { |
| 60 | if (mMediaCasServiceAidl != nullptr) { |
| 61 | SessionIntentAidl intent = SessionIntentAidl::LIVE; |
| 62 | ScramblingModeAidl mode = ScramblingModeAidl::RESERVED; |
| 63 | std::vector<uint8_t> sessionId; |
| 64 | ScopedAStatus status = mCasAidl->openSession(intent, mode, &sessionId); |
| 65 | if (!status.isOk()) { |
| 66 | ALOGW("[vts] Failed to open cas session for AIDL service."); |
| 67 | mCasAidl->closeSession(sessionId); |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 68 | return failure(); |
| 69 | } |
Yixiao Luo | e8730a7 | 2023-02-01 18:39:51 -0800 | [diff] [blame] | 70 | |
| 71 | if (pvtData.size() > 0) { |
| 72 | ScopedAStatus status = mCasAidl->setSessionPrivateData(sessionId, pvtData); |
| 73 | if (!status.isOk()) { |
| 74 | ALOGW("[vts] Failed to set session private data for AIDL service."); |
| 75 | mCasAidl->closeSession(sessionId); |
| 76 | return failure(); |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | Status sessionStatus; |
| 81 | SessionIntentHidl intent = SessionIntentHidl::LIVE; |
| 82 | ScramblingModeHidl mode = ScramblingModeHidl::RESERVED; |
| 83 | auto returnVoid = mCasHidl->openSession_1_2( |
| 84 | intent, mode, [&](Status status, const hidl_vec<uint8_t>& id) { |
| 85 | sessionStatus = status; |
| 86 | sessionId = id; |
| 87 | }); |
| 88 | if (!returnVoid.isOk() || (sessionStatus != Status::OK)) { |
| 89 | ALOGW("[vts] Failed to open cas session for HIDL service."); |
| 90 | mCasHidl->closeSession(sessionId); |
| 91 | return failure(); |
| 92 | } |
| 93 | |
| 94 | if (pvtData.size() > 0) { |
| 95 | auto status = mCasHidl->setSessionPrivateData(sessionId, pvtData); |
| 96 | if (status != android::hardware::cas::V1_0::Status::OK) { |
| 97 | ALOGW("[vts] Failed to set session private data for HIDL service."); |
| 98 | mCasHidl->closeSession(sessionId); |
| 99 | return failure(); |
| 100 | } |
| 101 | } |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return success(); |
| 105 | } |
| 106 | |
| 107 | AssertionResult DescramblerTests::getKeyToken(int32_t caSystemId, string& provisonStr, |
Yixiao Luo | e8730a7 | 2023-02-01 18:39:51 -0800 | [diff] [blame] | 108 | vector<uint8_t>& pvtData, vector<uint8_t>& token) { |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 109 | if (createCasPlugin(caSystemId) != success()) { |
| 110 | ALOGW("[vts] createCasPlugin failed."); |
| 111 | return failure(); |
| 112 | } |
| 113 | |
| 114 | if (provisonStr.size() > 0) { |
Yixiao Luo | e8730a7 | 2023-02-01 18:39:51 -0800 | [diff] [blame] | 115 | if (mMediaCasServiceAidl != nullptr) { |
| 116 | ScopedAStatus status = mCasAidl->provision(provisonStr); |
| 117 | if (!status.isOk()) { |
| 118 | ALOGW("[vts] provision failed for AIDL service."); |
| 119 | return failure(); |
| 120 | } |
| 121 | } else { |
| 122 | auto returnStatus = mCasHidl->provision(hidl_string(provisonStr)); |
| 123 | if (returnStatus != android::hardware::cas::V1_0::Status::OK) { |
| 124 | ALOGW("[vts] provision failed for HIDL service."); |
| 125 | return failure(); |
| 126 | } |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Yixiao Luo | e8730a7 | 2023-02-01 18:39:51 -0800 | [diff] [blame] | 130 | return openCasSession(token, pvtData); |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | AssertionResult DescramblerTests::openDescrambler(int32_t demuxId) { |
| 134 | ndk::ScopedAStatus status; |
| 135 | status = mService->openDescrambler(&mDescrambler); |
| 136 | if (!status.isOk()) { |
| 137 | ALOGW("[vts] openDescrambler failed."); |
| 138 | return failure(); |
| 139 | } |
| 140 | |
| 141 | status = mDescrambler->setDemuxSource(demuxId); |
| 142 | if (!status.isOk()) { |
| 143 | ALOGW("[vts] setDemuxSource failed."); |
| 144 | return failure(); |
| 145 | } |
| 146 | |
| 147 | return success(); |
| 148 | } |
| 149 | |
| 150 | AssertionResult DescramblerTests::setKeyToken(vector<uint8_t>& token) { |
| 151 | ndk::ScopedAStatus status; |
| 152 | if (!mDescrambler) { |
| 153 | ALOGW("[vts] Descrambler is not opened yet."); |
| 154 | return failure(); |
| 155 | } |
| 156 | |
| 157 | status = mDescrambler->setKeyToken(token); |
| 158 | if (!status.isOk()) { |
| 159 | ALOGW("[vts] setKeyToken failed."); |
| 160 | return failure(); |
| 161 | } |
| 162 | |
| 163 | return success(); |
| 164 | } |
| 165 | |
| 166 | AssertionResult DescramblerTests::addPid(DemuxPid pid, |
| 167 | std::shared_ptr<IFilter> optionalSourceFilter) { |
| 168 | ndk::ScopedAStatus status; |
| 169 | if (!mDescrambler) { |
| 170 | ALOGW("[vts] Descrambler is not opened yet."); |
| 171 | return failure(); |
| 172 | } |
| 173 | |
| 174 | status = mDescrambler->addPid(pid, optionalSourceFilter); |
| 175 | if (!status.isOk()) { |
| 176 | ALOGW("[vts] addPid failed."); |
| 177 | return failure(); |
| 178 | } |
| 179 | |
| 180 | return success(); |
| 181 | } |
| 182 | |
| 183 | AssertionResult DescramblerTests::removePid(DemuxPid pid, |
| 184 | std::shared_ptr<IFilter> optionalSourceFilter) { |
| 185 | ndk::ScopedAStatus status; |
| 186 | if (!mDescrambler) { |
| 187 | ALOGW("[vts] Descrambler is not opened yet."); |
| 188 | return failure(); |
| 189 | } |
| 190 | |
| 191 | status = mDescrambler->removePid(pid, optionalSourceFilter); |
| 192 | if (!status.isOk()) { |
| 193 | ALOGW("[vts] removePid failed."); |
| 194 | return failure(); |
| 195 | } |
| 196 | |
| 197 | return success(); |
| 198 | } |
| 199 | |
| 200 | AssertionResult DescramblerTests::closeDescrambler() { |
| 201 | ndk::ScopedAStatus status; |
| 202 | if (!mDescrambler) { |
| 203 | ALOGW("[vts] Descrambler is not opened yet."); |
| 204 | return failure(); |
| 205 | } |
| 206 | |
| 207 | status = mDescrambler->close(); |
| 208 | mDescrambler = nullptr; |
| 209 | if (!status.isOk()) { |
| 210 | ALOGW("[vts] close Descrambler failed."); |
| 211 | return failure(); |
| 212 | } |
| 213 | |
| 214 | return success(); |
| 215 | } |
| 216 | |
| 217 | AssertionResult DescramblerTests::getDemuxPidFromFilterSettings(DemuxFilterType type, |
| 218 | const DemuxFilterSettings& settings, |
| 219 | DemuxPid& pid) { |
| 220 | switch (type.mainType) { |
| 221 | case DemuxFilterMainType::TS: |
Hongguang | ce1e30d | 2021-08-02 21:55:44 -0700 | [diff] [blame] | 222 | if (type.subType.get<DemuxFilterSubType::Tag::tsFilterType>() == |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 223 | DemuxTsFilterType::AUDIO || |
Hongguang | ce1e30d | 2021-08-02 21:55:44 -0700 | [diff] [blame] | 224 | type.subType.get<DemuxFilterSubType::Tag::tsFilterType>() == |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 225 | DemuxTsFilterType::VIDEO) { |
| 226 | pid.set<DemuxPid::Tag::tPid>(settings.get<DemuxFilterSettings::Tag::ts>().tpid); |
| 227 | } else { |
| 228 | ALOGW("[vts] Not a media ts filter!"); |
| 229 | return failure(); |
| 230 | } |
| 231 | break; |
| 232 | case DemuxFilterMainType::MMTP: |
Hongguang | ce1e30d | 2021-08-02 21:55:44 -0700 | [diff] [blame] | 233 | if (type.subType.get<DemuxFilterSubType::Tag::mmtpFilterType>() == |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 234 | DemuxMmtpFilterType::AUDIO || |
Hongguang | ce1e30d | 2021-08-02 21:55:44 -0700 | [diff] [blame] | 235 | type.subType.get<DemuxFilterSubType::Tag::mmtpFilterType>() == |
Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 236 | DemuxMmtpFilterType::VIDEO) { |
| 237 | pid.set<DemuxPid::Tag::mmtpPid>( |
| 238 | settings.get<DemuxFilterSettings::Tag::mmtp>().mmtpPid); |
| 239 | } else { |
| 240 | ALOGW("[vts] Not a media mmtp filter!"); |
| 241 | return failure(); |
| 242 | } |
| 243 | break; |
| 244 | default: |
| 245 | ALOGW("[vts] Not a media filter!"); |
| 246 | return failure(); |
| 247 | } |
| 248 | return success(); |
| 249 | } |