Amy Zhang | b2989b5 | 2021-02-05 12:27:25 -0800 | [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 | #define LOG_TAG "TunerDescrambler" |
| 18 | |
| 19 | #include "TunerFilter.h" |
| 20 | #include "TunerDemux.h" |
| 21 | #include "TunerDescrambler.h" |
| 22 | |
| 23 | using ::android::hardware::tv::tuner::V1_0::Result; |
| 24 | |
| 25 | using namespace std; |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | TunerDescrambler::TunerDescrambler(sp<IDescrambler> descrambler) { |
| 30 | mDescrambler = descrambler; |
| 31 | } |
| 32 | |
| 33 | TunerDescrambler::~TunerDescrambler() { |
| 34 | mDescrambler = nullptr; |
| 35 | } |
| 36 | |
| 37 | Status TunerDescrambler::setDemuxSource(const std::shared_ptr<ITunerDemux>& demux) { |
| 38 | if (mDescrambler == nullptr) { |
| 39 | ALOGE("IDescrambler is not initialized"); |
| 40 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 41 | } |
| 42 | |
| 43 | Result res = mDescrambler->setDemuxSource(static_cast<TunerDemux*>(demux.get())->getId()); |
| 44 | if (res != Result::SUCCESS) { |
| 45 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 46 | } |
| 47 | return Status::ok(); |
| 48 | } |
| 49 | |
| 50 | Status TunerDescrambler::setKeyToken(const vector<uint8_t>& keyToken) { |
| 51 | if (mDescrambler == nullptr) { |
| 52 | ALOGE("IDescrambler is not initialized"); |
| 53 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 54 | } |
| 55 | |
| 56 | Result res = mDescrambler->setKeyToken(keyToken); |
| 57 | if (res != Result::SUCCESS) { |
| 58 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 59 | } |
| 60 | return Status::ok(); |
| 61 | } |
| 62 | |
| 63 | Status TunerDescrambler::addPid(const TunerDemuxPid& pid, |
| 64 | const shared_ptr<ITunerFilter>& optionalSourceFilter) { |
| 65 | if (mDescrambler == nullptr) { |
| 66 | ALOGE("IDescrambler is not initialized"); |
| 67 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 68 | } |
| 69 | |
| 70 | Result res = mDescrambler->addPid(getHidlDemuxPid(pid), |
| 71 | static_cast<TunerFilter*>(optionalSourceFilter.get())->getHalFilter()); |
| 72 | if (res != Result::SUCCESS) { |
| 73 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 74 | } |
| 75 | return Status::ok(); |
| 76 | } |
| 77 | |
| 78 | Status TunerDescrambler::removePid(const TunerDemuxPid& pid, |
| 79 | const shared_ptr<ITunerFilter>& optionalSourceFilter) { |
| 80 | if (mDescrambler == nullptr) { |
| 81 | ALOGE("IDescrambler is not initialized"); |
| 82 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 83 | } |
| 84 | |
| 85 | Result res = mDescrambler->removePid(getHidlDemuxPid(pid), |
| 86 | static_cast<TunerFilter*>(optionalSourceFilter.get())->getHalFilter()); |
| 87 | if (res != Result::SUCCESS) { |
| 88 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 89 | } |
| 90 | return Status::ok(); |
| 91 | } |
| 92 | |
| 93 | Status TunerDescrambler::close() { |
| 94 | if (mDescrambler == nullptr) { |
| 95 | ALOGE("IDescrambler is not initialized."); |
| 96 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 97 | } |
| 98 | |
| 99 | Result res = mDescrambler->close(); |
| 100 | if (res != Result::SUCCESS) { |
| 101 | return Status::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 102 | } |
| 103 | return Status::ok(); |
| 104 | } |
| 105 | |
| 106 | DemuxPid TunerDescrambler::getHidlDemuxPid(const TunerDemuxPid& pid) { |
| 107 | DemuxPid hidlPid; |
| 108 | switch (pid.getTag()) { |
| 109 | case TunerDemuxPid::tPid: { |
| 110 | hidlPid.tPid((uint16_t)pid.tPid); |
| 111 | break; |
| 112 | } |
| 113 | case TunerDemuxPid::mmtpPid: { |
| 114 | hidlPid.mmtpPid((uint16_t)pid.mmtpPid); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | return hidlPid; |
| 119 | } |
| 120 | } // namespace android |