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 | |
Amy Zhang | deafc7f | 2021-04-08 12:24:57 -0700 | [diff] [blame] | 70 | sp<IFilter> halFilter = (optionalSourceFilter == NULL) |
| 71 | ? NULL : static_cast<TunerFilter*>(optionalSourceFilter.get())->getHalFilter(); |
| 72 | Result res = mDescrambler->addPid(getHidlDemuxPid(pid), halFilter); |
Amy Zhang | b2989b5 | 2021-02-05 12:27:25 -0800 | [diff] [blame] | 73 | if (res != Result::SUCCESS) { |
| 74 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 75 | } |
| 76 | return Status::ok(); |
| 77 | } |
| 78 | |
| 79 | Status TunerDescrambler::removePid(const TunerDemuxPid& pid, |
| 80 | const shared_ptr<ITunerFilter>& optionalSourceFilter) { |
| 81 | if (mDescrambler == nullptr) { |
| 82 | ALOGE("IDescrambler is not initialized"); |
| 83 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 84 | } |
| 85 | |
Amy Zhang | deafc7f | 2021-04-08 12:24:57 -0700 | [diff] [blame] | 86 | sp<IFilter> halFilter = (optionalSourceFilter == NULL) |
| 87 | ? NULL : static_cast<TunerFilter*>(optionalSourceFilter.get())->getHalFilter(); |
| 88 | Result res = mDescrambler->removePid(getHidlDemuxPid(pid), halFilter); |
Amy Zhang | b2989b5 | 2021-02-05 12:27:25 -0800 | [diff] [blame] | 89 | if (res != Result::SUCCESS) { |
| 90 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 91 | } |
| 92 | return Status::ok(); |
| 93 | } |
| 94 | |
| 95 | Status TunerDescrambler::close() { |
| 96 | if (mDescrambler == nullptr) { |
| 97 | ALOGE("IDescrambler is not initialized."); |
| 98 | return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE)); |
| 99 | } |
| 100 | |
| 101 | Result res = mDescrambler->close(); |
Amy Zhang | 14a60e8 | 2021-02-11 15:22:52 -0800 | [diff] [blame] | 102 | mDescrambler = NULL; |
| 103 | |
Amy Zhang | b2989b5 | 2021-02-05 12:27:25 -0800 | [diff] [blame] | 104 | if (res != Result::SUCCESS) { |
| 105 | return Status::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 106 | } |
| 107 | return Status::ok(); |
| 108 | } |
| 109 | |
| 110 | DemuxPid TunerDescrambler::getHidlDemuxPid(const TunerDemuxPid& pid) { |
| 111 | DemuxPid hidlPid; |
| 112 | switch (pid.getTag()) { |
| 113 | case TunerDemuxPid::tPid: { |
Amy Zhang | 432b123 | 2021-04-16 16:23:39 -0700 | [diff] [blame] | 114 | hidlPid.tPid((uint16_t)pid.get<TunerDemuxPid::tPid>()); |
Amy Zhang | b2989b5 | 2021-02-05 12:27:25 -0800 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | case TunerDemuxPid::mmtpPid: { |
Amy Zhang | 432b123 | 2021-04-16 16:23:39 -0700 | [diff] [blame] | 118 | hidlPid.mmtpPid((uint16_t)pid.get<TunerDemuxPid::mmtpPid>()); |
Amy Zhang | b2989b5 | 2021-02-05 12:27:25 -0800 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | } |
| 122 | return hidlPid; |
| 123 | } |
| 124 | } // namespace android |