blob: 157fa0479eafe7ed0329f43cffdb6f2f746cc749 [file] [log] [blame]
Hongguang600a6ae2021-07-08 18:51:51 -07001/*
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
19using namespace std;
20
21AssertionResult DescramblerTests::createCasPlugin(int32_t caSystemId) {
22 auto status = mMediaCasService->isSystemIdSupported(caSystemId);
23 if (!status.isOk() || !status) {
24 ALOGW("[vts] Failed to check isSystemIdSupported.");
25 return failure();
26 }
27
28 mCasListener = new MediaCasListener();
29 auto pluginStatus = mMediaCasService->createPluginExt(caSystemId, mCasListener);
30 if (!pluginStatus.isOk()) {
31 ALOGW("[vts] Failed to createPluginExt.");
32 return failure();
33 }
34 mCas = ICas::castFrom(pluginStatus);
35 if (mCas == nullptr) {
36 ALOGW("[vts] Failed to get ICas.");
37 return failure();
38 }
39 return success();
40}
41
42AssertionResult DescramblerTests::openCasSession(vector<uint8_t>& sessionId,
43 vector<uint8_t>& hidlPvtData) {
44 Status sessionStatus;
45 SessionIntent intent = SessionIntent::LIVE;
46 ScramblingMode mode = ScramblingMode::RESERVED;
47 auto returnVoid =
48 mCas->openSession_1_2(intent, mode, [&](Status status, const hidl_vec<uint8_t>& id) {
49 sessionStatus = status;
50 sessionId = id;
51 });
52 if (!returnVoid.isOk() || (sessionStatus != Status::OK)) {
53 ALOGW("[vts] Failed to open cas session.");
54 mCas->closeSession(sessionId);
55 return failure();
56 }
57
58 if (hidlPvtData.size() > 0) {
59 auto status = mCas->setSessionPrivateData(sessionId, hidlPvtData);
60 if (status != android::hardware::cas::V1_0::Status::OK) {
61 ALOGW("[vts] Failed to set session private data");
62 mCas->closeSession(sessionId);
63 return failure();
64 }
65 }
66
67 return success();
68}
69
70AssertionResult DescramblerTests::getKeyToken(int32_t caSystemId, string& provisonStr,
71 vector<uint8_t>& hidlPvtData,
72 vector<uint8_t>& token) {
73 if (createCasPlugin(caSystemId) != success()) {
74 ALOGW("[vts] createCasPlugin failed.");
75 return failure();
76 }
77
78 if (provisonStr.size() > 0) {
79 auto returnStatus = mCas->provision(hidl_string(provisonStr));
80 if (returnStatus != android::hardware::cas::V1_0::Status::OK) {
81 ALOGW("[vts] provision failed.");
82 return failure();
83 }
84 }
85
86 return openCasSession(token, hidlPvtData);
87}
88
89AssertionResult DescramblerTests::openDescrambler(int32_t demuxId) {
90 ndk::ScopedAStatus status;
91 status = mService->openDescrambler(&mDescrambler);
92 if (!status.isOk()) {
93 ALOGW("[vts] openDescrambler failed.");
94 return failure();
95 }
96
97 status = mDescrambler->setDemuxSource(demuxId);
98 if (!status.isOk()) {
99 ALOGW("[vts] setDemuxSource failed.");
100 return failure();
101 }
102
103 return success();
104}
105
106AssertionResult DescramblerTests::setKeyToken(vector<uint8_t>& token) {
107 ndk::ScopedAStatus status;
108 if (!mDescrambler) {
109 ALOGW("[vts] Descrambler is not opened yet.");
110 return failure();
111 }
112
113 status = mDescrambler->setKeyToken(token);
114 if (!status.isOk()) {
115 ALOGW("[vts] setKeyToken failed.");
116 return failure();
117 }
118
119 return success();
120}
121
122AssertionResult DescramblerTests::addPid(DemuxPid pid,
123 std::shared_ptr<IFilter> optionalSourceFilter) {
124 ndk::ScopedAStatus status;
125 if (!mDescrambler) {
126 ALOGW("[vts] Descrambler is not opened yet.");
127 return failure();
128 }
129
130 status = mDescrambler->addPid(pid, optionalSourceFilter);
131 if (!status.isOk()) {
132 ALOGW("[vts] addPid failed.");
133 return failure();
134 }
135
136 return success();
137}
138
139AssertionResult DescramblerTests::removePid(DemuxPid pid,
140 std::shared_ptr<IFilter> optionalSourceFilter) {
141 ndk::ScopedAStatus status;
142 if (!mDescrambler) {
143 ALOGW("[vts] Descrambler is not opened yet.");
144 return failure();
145 }
146
147 status = mDescrambler->removePid(pid, optionalSourceFilter);
148 if (!status.isOk()) {
149 ALOGW("[vts] removePid failed.");
150 return failure();
151 }
152
153 return success();
154}
155
156AssertionResult DescramblerTests::closeDescrambler() {
157 ndk::ScopedAStatus status;
158 if (!mDescrambler) {
159 ALOGW("[vts] Descrambler is not opened yet.");
160 return failure();
161 }
162
163 status = mDescrambler->close();
164 mDescrambler = nullptr;
165 if (!status.isOk()) {
166 ALOGW("[vts] close Descrambler failed.");
167 return failure();
168 }
169
170 return success();
171}
172
173AssertionResult DescramblerTests::getDemuxPidFromFilterSettings(DemuxFilterType type,
174 const DemuxFilterSettings& settings,
175 DemuxPid& pid) {
176 switch (type.mainType) {
177 case DemuxFilterMainType::TS:
Hongguangce1e30d2021-08-02 21:55:44 -0700178 if (type.subType.get<DemuxFilterSubType::Tag::tsFilterType>() ==
Hongguang600a6ae2021-07-08 18:51:51 -0700179 DemuxTsFilterType::AUDIO ||
Hongguangce1e30d2021-08-02 21:55:44 -0700180 type.subType.get<DemuxFilterSubType::Tag::tsFilterType>() ==
Hongguang600a6ae2021-07-08 18:51:51 -0700181 DemuxTsFilterType::VIDEO) {
182 pid.set<DemuxPid::Tag::tPid>(settings.get<DemuxFilterSettings::Tag::ts>().tpid);
183 } else {
184 ALOGW("[vts] Not a media ts filter!");
185 return failure();
186 }
187 break;
188 case DemuxFilterMainType::MMTP:
Hongguangce1e30d2021-08-02 21:55:44 -0700189 if (type.subType.get<DemuxFilterSubType::Tag::mmtpFilterType>() ==
Hongguang600a6ae2021-07-08 18:51:51 -0700190 DemuxMmtpFilterType::AUDIO ||
Hongguangce1e30d2021-08-02 21:55:44 -0700191 type.subType.get<DemuxFilterSubType::Tag::mmtpFilterType>() ==
Hongguang600a6ae2021-07-08 18:51:51 -0700192 DemuxMmtpFilterType::VIDEO) {
193 pid.set<DemuxPid::Tag::mmtpPid>(
194 settings.get<DemuxFilterSettings::Tag::mmtp>().mmtpPid);
195 } else {
196 ALOGW("[vts] Not a media mmtp filter!");
197 return failure();
198 }
199 break;
200 default:
201 ALOGW("[vts] Not a media filter!");
202 return failure();
203 }
204 return success();
205}