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