Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [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 "TunerHidlDescrambler" |
| 18 | |
| 19 | #include "TunerHidlDescrambler.h" |
| 20 | |
| 21 | #include <aidl/android/hardware/tv/tuner/Result.h> |
| 22 | |
| 23 | #include "TunerHidlDemux.h" |
| 24 | #include "TunerHidlFilter.h" |
| 25 | |
| 26 | using ::aidl::android::hardware::tv::tuner::Result; |
| 27 | |
| 28 | using HidlResult = ::android::hardware::tv::tuner::V1_0::Result; |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | namespace aidl { |
| 33 | namespace android { |
| 34 | namespace media { |
| 35 | namespace tv { |
| 36 | namespace tuner { |
| 37 | |
| 38 | TunerHidlDescrambler::TunerHidlDescrambler(sp<HidlIDescrambler> descrambler) { |
| 39 | mDescrambler = descrambler; |
| 40 | } |
| 41 | |
| 42 | TunerHidlDescrambler::~TunerHidlDescrambler() { |
| 43 | mDescrambler = nullptr; |
| 44 | } |
| 45 | |
| 46 | ::ndk::ScopedAStatus TunerHidlDescrambler::setDemuxSource( |
| 47 | const shared_ptr<ITunerDemux>& in_tunerDemux) { |
| 48 | if (mDescrambler == nullptr) { |
| 49 | ALOGE("IDescrambler is not initialized"); |
| 50 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 51 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 52 | } |
| 53 | |
| 54 | HidlResult res = mDescrambler->setDemuxSource( |
| 55 | static_cast<TunerHidlDemux*>(in_tunerDemux.get())->getId()); |
| 56 | if (res != HidlResult::SUCCESS) { |
| 57 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 58 | } |
| 59 | return ::ndk::ScopedAStatus::ok(); |
| 60 | } |
| 61 | |
| 62 | ::ndk::ScopedAStatus TunerHidlDescrambler::setKeyToken(const vector<uint8_t>& in_keyToken) { |
| 63 | if (mDescrambler == nullptr) { |
| 64 | ALOGE("IDescrambler is not initialized"); |
| 65 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 66 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 67 | } |
| 68 | |
| 69 | HidlResult res = mDescrambler->setKeyToken(in_keyToken); |
| 70 | if (res != HidlResult::SUCCESS) { |
| 71 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 72 | } |
| 73 | return ::ndk::ScopedAStatus::ok(); |
| 74 | } |
| 75 | |
| 76 | ::ndk::ScopedAStatus TunerHidlDescrambler::addPid( |
| 77 | const DemuxPid& in_pid, const shared_ptr<ITunerFilter>& in_optionalSourceFilter) { |
| 78 | if (mDescrambler == nullptr) { |
| 79 | ALOGE("IDescrambler is not initialized"); |
| 80 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 81 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 82 | } |
| 83 | |
| 84 | sp<HidlIFilter> halFilter = |
| 85 | (in_optionalSourceFilter == nullptr) |
| 86 | ? nullptr |
| 87 | : static_cast<TunerHidlFilter*>(in_optionalSourceFilter.get())->getHalFilter(); |
| 88 | HidlResult res = mDescrambler->addPid(getHidlDemuxPid(in_pid), halFilter); |
| 89 | if (res != HidlResult::SUCCESS) { |
| 90 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 91 | } |
| 92 | return ::ndk::ScopedAStatus::ok(); |
| 93 | } |
| 94 | |
| 95 | ::ndk::ScopedAStatus TunerHidlDescrambler::removePid( |
| 96 | const DemuxPid& in_pid, const shared_ptr<ITunerFilter>& in_optionalSourceFilter) { |
| 97 | if (mDescrambler == nullptr) { |
| 98 | ALOGE("IDescrambler is not initialized"); |
| 99 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 100 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 101 | } |
| 102 | |
| 103 | sp<HidlIFilter> halFilter = |
| 104 | (in_optionalSourceFilter == nullptr) |
| 105 | ? nullptr |
| 106 | : static_cast<TunerHidlFilter*>(in_optionalSourceFilter.get())->getHalFilter(); |
| 107 | HidlResult res = mDescrambler->removePid(getHidlDemuxPid(in_pid), halFilter); |
| 108 | if (res != HidlResult::SUCCESS) { |
| 109 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 110 | } |
| 111 | return ::ndk::ScopedAStatus::ok(); |
| 112 | } |
| 113 | |
| 114 | ::ndk::ScopedAStatus TunerHidlDescrambler::close() { |
| 115 | if (mDescrambler == nullptr) { |
| 116 | ALOGE("IDescrambler is not initialized."); |
| 117 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 118 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 119 | } |
| 120 | |
| 121 | HidlResult res = mDescrambler->close(); |
| 122 | mDescrambler = nullptr; |
| 123 | |
| 124 | if (res != HidlResult::SUCCESS) { |
| 125 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 126 | } |
| 127 | return ::ndk::ScopedAStatus::ok(); |
| 128 | } |
| 129 | |
| 130 | HidlDemuxPid TunerHidlDescrambler::getHidlDemuxPid(const DemuxPid& pid) { |
| 131 | HidlDemuxPid hidlPid; |
| 132 | switch (pid.getTag()) { |
| 133 | case DemuxPid::tPid: { |
| 134 | hidlPid.tPid((uint16_t)pid.get<DemuxPid::Tag::tPid>()); |
| 135 | break; |
| 136 | } |
| 137 | case DemuxPid::mmtpPid: { |
| 138 | hidlPid.mmtpPid((uint16_t)pid.get<DemuxPid::Tag::mmtpPid>()); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | return hidlPid; |
| 143 | } |
| 144 | |
| 145 | } // namespace tuner |
| 146 | } // namespace tv |
| 147 | } // namespace media |
| 148 | } // namespace android |
| 149 | } // namespace aidl |