• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "DescramblerClient"
18 
19 #include <android-base/logging.h>
20 #include <utils/Log.h>
21 
22 #include "DescramblerClient.h"
23 
24 using ::android::hardware::tv::tuner::V1_0::Result;
25 
26 namespace android {
27 
28 /////////////// DescramblerClient ///////////////////////
29 
DescramblerClient(shared_ptr<ITunerDescrambler> tunerDescrambler)30 DescramblerClient::DescramblerClient(shared_ptr<ITunerDescrambler> tunerDescrambler) {
31     mTunerDescrambler = tunerDescrambler;
32 }
33 
~DescramblerClient()34 DescramblerClient::~DescramblerClient() {
35     mTunerDescrambler = NULL;
36     mDescrambler = NULL;
37 }
38 
39 // TODO: remove after migration to Tuner Service is done.
setHidlDescrambler(sp<IDescrambler> descrambler)40 void DescramblerClient::setHidlDescrambler(sp<IDescrambler> descrambler) {
41     mDescrambler = descrambler;
42 }
43 
setDemuxSource(sp<DemuxClient> demuxClient)44 Result DescramblerClient::setDemuxSource(sp<DemuxClient> demuxClient) {
45     if (demuxClient == NULL) {
46         return Result::INVALID_ARGUMENT;
47     }
48 
49     if (mTunerDescrambler != NULL) {
50         Status s = mTunerDescrambler->setDemuxSource(demuxClient->getAidlDemux());
51         return ClientHelper::getServiceSpecificErrorCode(s);
52     }
53 
54     if (mDescrambler != NULL) {
55         return mDescrambler->setDemuxSource(demuxClient->getId());
56     }
57 
58     return Result::INVALID_STATE;
59 }
60 
setKeyToken(vector<uint8_t> keyToken)61 Result DescramblerClient::setKeyToken(vector<uint8_t> keyToken) {
62     if (mTunerDescrambler != NULL) {
63         Status s = mTunerDescrambler->setKeyToken(keyToken);
64         return ClientHelper::getServiceSpecificErrorCode(s);
65     }
66 
67     if (mDescrambler != NULL) {
68         return mDescrambler->setKeyToken(keyToken);
69     }
70 
71     return Result::INVALID_STATE;
72 }
73 
addPid(DemuxPid pid,sp<FilterClient> optionalSourceFilter)74 Result DescramblerClient::addPid(DemuxPid pid, sp<FilterClient> optionalSourceFilter) {
75     if (mTunerDescrambler != NULL) {
76         shared_ptr<ITunerFilter> aidlFilter = (optionalSourceFilter == NULL)
77                 ? NULL : optionalSourceFilter->getAidlFilter();
78         Status s = mTunerDescrambler->addPid(getAidlDemuxPid(pid), aidlFilter);
79         return ClientHelper::getServiceSpecificErrorCode(s);
80     }
81 
82     if (mDescrambler != NULL) {
83         sp<IFilter> halFilter = (optionalSourceFilter == NULL)
84                 ? NULL : optionalSourceFilter->getHalFilter();
85         return mDescrambler->addPid(pid, halFilter);
86     }
87 
88     return Result::INVALID_STATE;
89 }
90 
removePid(DemuxPid pid,sp<FilterClient> optionalSourceFilter)91 Result DescramblerClient::removePid(DemuxPid pid, sp<FilterClient> optionalSourceFilter) {
92     if (mTunerDescrambler != NULL) {
93         shared_ptr<ITunerFilter> aidlFilter = (optionalSourceFilter == NULL)
94                 ? NULL : optionalSourceFilter->getAidlFilter();
95         Status s = mTunerDescrambler->removePid(getAidlDemuxPid(pid), aidlFilter);
96         return ClientHelper::getServiceSpecificErrorCode(s);
97     }
98 
99     if (mDescrambler != NULL) {
100         sp<IFilter> halFilter = (optionalSourceFilter == NULL)
101                 ? NULL : optionalSourceFilter->getHalFilter();
102         return mDescrambler->removePid(pid, halFilter);
103     }
104 
105     return Result::INVALID_STATE;
106 }
107 
close()108 Result DescramblerClient::close() {
109     if (mTunerDescrambler != NULL) {
110         Status s = mTunerDescrambler->close();
111         mTunerDescrambler = NULL;
112         return ClientHelper::getServiceSpecificErrorCode(s);
113     }
114 
115     if (mDescrambler != NULL) {
116         Result res = mDescrambler->close();
117         mDescrambler = NULL;
118         return res;
119     }
120 
121     return Result::INVALID_STATE;
122 }
123 
124 /////////////// DescramblerClient Helper Methods ///////////////////////
125 
getAidlDemuxPid(DemuxPid & pid)126 TunerDemuxPid DescramblerClient::getAidlDemuxPid(DemuxPid& pid) {
127     TunerDemuxPid aidlPid;
128     switch (pid.getDiscriminator()) {
129         case DemuxPid::hidl_discriminator::tPid:
130             aidlPid.set<TunerDemuxPid::tPid>((int)pid.tPid());
131             break;
132         case DemuxPid::hidl_discriminator::mmtpPid:
133             aidlPid.set<TunerDemuxPid::mmtpPid>((int)pid.mmtpPid());
134             break;
135     }
136     return aidlPid;
137 }
138 }  // namespace android
139