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     auto status = mCas->setSessionPrivateData(sessionId, hidlPvtData);
57     if (status != android::hardware::cas::V1_0::Status::OK) {
58         ALOGW("[vts] Failed to set session private data");
59         mCas->closeSession(sessionId);
60         return failure();
61     }
62     return success();
63 }
64 
getKeyToken(int32_t caSystemId,string provisonStr,hidl_vec<uint8_t> hidlPvtData,TunerKeyToken & token)65 AssertionResult DescramblerTests::getKeyToken(int32_t caSystemId, string provisonStr,
66                                               hidl_vec<uint8_t> hidlPvtData, TunerKeyToken& token) {
67     if (createCasPlugin(caSystemId) != success()) {
68         ALOGW("[vts] createCasPlugin failed.");
69         return failure();
70     }
71 
72     if (provisonStr.size() > 0) {
73         auto returnStatus = mCas->provision(hidl_string(provisonStr));
74         if (returnStatus != android::hardware::cas::V1_0::Status::OK) {
75             ALOGW("[vts] provision failed.");
76             return failure();
77         }
78     }
79 
80     return openCasSession(token, hidlPvtData);
81 }
82 
openDescrambler(uint32_t demuxId)83 AssertionResult DescramblerTests::openDescrambler(uint32_t demuxId) {
84     Result status;
85     mService->openDescrambler([&](Result result, const sp<IDescrambler>& descrambler) {
86         mDescrambler = descrambler;
87         status = result;
88     });
89     if (status != Result::SUCCESS) {
90         ALOGW("[vts] openDescrambler failed.");
91         return failure();
92     }
93 
94     status = mDescrambler->setDemuxSource(demuxId);
95     if (status != Result::SUCCESS) {
96         ALOGW("[vts] setDemuxSource failed.");
97         return failure();
98     }
99 
100     return success();
101 }
102 
setKeyToken(TunerKeyToken token)103 AssertionResult DescramblerTests::setKeyToken(TunerKeyToken token) {
104     Result status;
105     if (mDescrambler) {
106         ALOGW("[vts] Descrambler is not opened yet.");
107         return failure();
108     }
109 
110     status = mDescrambler->setKeyToken(token);
111     if (status == Result::SUCCESS) {
112         ALOGW("[vts] setKeyToken failed.");
113         return failure();
114     }
115 
116     return success();
117 }
118 
addPid(DemuxPid pid,sp<IFilter> optionalSourceFilter)119 AssertionResult DescramblerTests::addPid(DemuxPid pid, sp<IFilter> optionalSourceFilter) {
120     Result status;
121     if (mDescrambler) {
122         ALOGW("[vts] Descrambler is not opened yet.");
123         return failure();
124     }
125 
126     status = mDescrambler->addPid(pid, optionalSourceFilter);
127     if (status == Result::SUCCESS) {
128         ALOGW("[vts] addPid failed.");
129         return failure();
130     }
131 
132     return success();
133 }
134 
removePid(DemuxPid pid,sp<IFilter> optionalSourceFilter)135 AssertionResult DescramblerTests::removePid(DemuxPid pid, sp<IFilter> optionalSourceFilter) {
136     Result status;
137     if (mDescrambler) {
138         ALOGW("[vts] Descrambler is not opened yet.");
139         return failure();
140     }
141 
142     status = mDescrambler->removePid(pid, optionalSourceFilter);
143     if (status == Result::SUCCESS) {
144         ALOGW("[vts] removePid failed.");
145         return failure();
146     }
147 
148     return success();
149 }
150 
closeDescrambler()151 AssertionResult DescramblerTests::closeDescrambler() {
152     Result status;
153     if (mDescrambler) {
154         ALOGW("[vts] Descrambler is not opened yet.");
155         return failure();
156     }
157 
158     status = mDescrambler->close();
159     mDescrambler = nullptr;
160     if (status == Result::SUCCESS) {
161         ALOGW("[vts] close Descrambler failed.");
162         return failure();
163     }
164 
165     return success();
166 }
167 
getDemuxPidFromFilterSettings(DemuxFilterType type,DemuxFilterSettings settings,DemuxPid & pid)168 AssertionResult DescramblerTests::getDemuxPidFromFilterSettings(DemuxFilterType type,
169                                                                 DemuxFilterSettings settings,
170                                                                 DemuxPid& pid) {
171     switch (type.mainType) {
172         case DemuxFilterMainType::TS:
173             if (type.subType.tsFilterType() == DemuxTsFilterType::AUDIO ||
174                 type.subType.tsFilterType() == DemuxTsFilterType::VIDEO) {
175                 pid.tPid(settings.ts().tpid);
176             } else {
177                 ALOGW("[vts] Not a media ts filter!");
178                 return failure();
179             }
180             break;
181         case DemuxFilterMainType::MMTP:
182             if (type.subType.mmtpFilterType() == DemuxMmtpFilterType::AUDIO ||
183                 type.subType.mmtpFilterType() == DemuxMmtpFilterType::VIDEO) {
184                 pid.mmtpPid(settings.mmtp().mmtpPid);
185             } else {
186                 ALOGW("[vts] Not a media mmtp filter!");
187                 return failure();
188             }
189             break;
190         default:
191             ALOGW("[vts] Not a media filter!");
192             return failure();
193     }
194     return success();
195 }
196