Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #ifndef ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_ |
| 18 | #define ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_ |
| 19 | |
| 20 | #include <android/hardware/tv/tuner/1.0/IDemux.h> |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 21 | #include <fmq/MessageQueue.h> |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 22 | #include <set> |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace tv { |
| 29 | namespace tuner { |
| 30 | namespace V1_0 { |
| 31 | namespace implementation { |
| 32 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 33 | using ::android::hardware::EventFlag; |
| 34 | using ::android::hardware::kSynchronizedReadWrite; |
| 35 | using ::android::hardware::MessageQueue; |
| 36 | using ::android::hardware::MQDescriptorSync; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 37 | using ::android::hardware::tv::tuner::V1_0::IDemux; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 38 | using ::android::hardware::tv::tuner::V1_0::IDemuxCallback; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 39 | using ::android::hardware::tv::tuner::V1_0::Result; |
| 40 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 41 | using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; |
| 42 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 43 | class Demux : public IDemux { |
| 44 | public: |
| 45 | Demux(uint32_t demuxId); |
| 46 | |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 47 | ~Demux(); |
| 48 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 49 | virtual Return<Result> setFrontendDataSource(uint32_t frontendId) override; |
| 50 | |
| 51 | virtual Return<Result> close() override; |
| 52 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 53 | virtual Return<void> addFilter(DemuxFilterType type, uint32_t bufferSize, |
| 54 | const sp<IDemuxCallback>& cb, addFilter_cb _hidl_cb) override; |
| 55 | |
| 56 | virtual Return<void> getFilterQueueDesc(uint32_t filterId, |
| 57 | getFilterQueueDesc_cb _hidl_cb) override; |
| 58 | |
| 59 | virtual Return<Result> configureFilter(uint32_t filterId, |
| 60 | const DemuxFilterSettings& settings) override; |
| 61 | |
| 62 | virtual Return<Result> startFilter(uint32_t filterId) override; |
| 63 | |
| 64 | virtual Return<Result> stopFilter(uint32_t filterId) override; |
| 65 | |
| 66 | virtual Return<Result> flushFilter(uint32_t filterId) override; |
| 67 | |
| 68 | virtual Return<Result> removeFilter(uint32_t filterId) override; |
| 69 | |
| 70 | virtual Return<void> getAvSyncHwId(uint32_t filterId, getAvSyncHwId_cb _hidl_cb) override; |
| 71 | |
| 72 | virtual Return<void> getAvSyncTime(AvSyncHwId avSyncHwId, getAvSyncTime_cb _hidl_cb) override; |
| 73 | |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 74 | virtual Return<Result> addInput(uint32_t bufferSize, const sp<IDemuxCallback>& cb) override; |
| 75 | |
| 76 | virtual Return<void> getInputQueueDesc(getInputQueueDesc_cb _hidl_cb) override; |
| 77 | |
| 78 | virtual Return<Result> configureInput(const DemuxInputSettings& settings) override; |
| 79 | |
| 80 | virtual Return<Result> startInput() override; |
| 81 | |
| 82 | virtual Return<Result> stopInput() override; |
| 83 | |
| 84 | virtual Return<Result> flushInput() override; |
| 85 | |
| 86 | virtual Return<Result> removeInput() override; |
| 87 | |
| 88 | virtual Return<Result> addOutput(uint32_t bufferSize, const sp<IDemuxCallback>& cb) override; |
| 89 | |
| 90 | virtual Return<void> getOutputQueueDesc(getOutputQueueDesc_cb _hidl_cb) override; |
| 91 | |
| 92 | virtual Return<Result> configureOutput(const DemuxOutputSettings& settings) override; |
| 93 | |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 94 | virtual Return<Result> attachOutputFilter(uint32_t filterId) override; |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 95 | |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 96 | virtual Return<Result> detachOutputFilter(uint32_t filterId) override; |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 97 | |
| 98 | virtual Return<Result> startOutput() override; |
| 99 | |
| 100 | virtual Return<Result> stopOutput() override; |
| 101 | |
| 102 | virtual Return<Result> flushOutput() override; |
| 103 | |
| 104 | virtual Return<Result> removeOutput() override; |
| 105 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 106 | private: |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 107 | // A struct that passes the arguments to a newly created filter thread |
| 108 | struct ThreadArgs { |
| 109 | Demux* user; |
| 110 | uint32_t filterId; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Filter handlers to handle the data filtering. |
| 115 | * They are also responsible to write the filtered output into the filter FMQ |
| 116 | * and update the filterEvent bound with the same filterId. |
| 117 | */ |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 118 | Result startSectionFilterHandler(uint32_t filterId); |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 119 | Result startPesFilterHandler(uint32_t filterId); |
| 120 | Result startTsFilterHandler(); |
| 121 | Result startMediaFilterHandler(uint32_t filterId); |
| 122 | Result startRecordFilterHandler(uint32_t filterId); |
| 123 | Result startPcrFilterHandler(); |
| 124 | Result startFilterLoop(uint32_t filterId); |
| 125 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 126 | /** |
| 127 | * To create a FilterMQ with the the next available Filter ID. |
| 128 | * Creating Event Flag at the same time. |
| 129 | * Add the successfully created/saved FilterMQ into the local list. |
| 130 | * |
| 131 | * Return false is any of the above processes fails. |
| 132 | */ |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 133 | bool createFilterMQ(uint32_t bufferSize, uint32_t filterId); |
| 134 | bool createMQ(FilterMQ* queue, EventFlag* eventFlag, uint32_t bufferSize); |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 135 | void deleteEventFlag(); |
| 136 | bool writeDataToFilterMQ(const std::vector<uint8_t>& data, uint32_t filterId); |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 137 | bool readDataFromMQ(); |
| 138 | bool writeSectionsAndCreateEvent(uint32_t filterId, vector<uint8_t> data); |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 139 | void maySendInputStatusCallback(); |
| 140 | DemuxInputStatus checkStatusChange(uint32_t availableToWrite, uint32_t availableToRead, |
| 141 | uint32_t highThreshold, uint32_t lowThreshold); |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 142 | /** |
| 143 | * A dispatcher to read and dispatch input data to all the started filters. |
| 144 | * Each filter handler handles the data filtering/output writing/filterEvent updating. |
| 145 | */ |
| 146 | bool filterAndOutputData(); |
| 147 | static void* __threadLoopFilter(void* data); |
| 148 | static void* __threadLoopInput(void* user); |
| 149 | void filterThreadLoop(uint32_t filterId); |
| 150 | void inputThreadLoop(); |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 151 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 152 | uint32_t mDemuxId; |
| 153 | uint32_t mSourceFrontendId; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 154 | /** |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 155 | * Record the last used filter id. Initial value is -1. |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 156 | * Filter Id starts with 0. |
| 157 | */ |
| 158 | uint32_t mLastUsedFilterId = -1; |
| 159 | /** |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 160 | * Record all the used filter Ids. |
| 161 | * Any removed filter id should be removed from this set. |
| 162 | */ |
| 163 | set<uint32_t> mUsedFilterIds; |
| 164 | /** |
| 165 | * Record all the unused filter Ids within mLastUsedFilterId. |
| 166 | * Removed filter Id should be added into this set. |
| 167 | * When this set is not empty, ids here should be allocated first |
| 168 | * and added into usedFilterIds. |
| 169 | */ |
| 170 | set<uint32_t> mUnusedFilterIds; |
| 171 | /** |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 172 | * A list of created FilterMQ ptrs. |
| 173 | * The array number is the filter ID. |
| 174 | */ |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 175 | vector<uint16_t> mFilterPids; |
| 176 | vector<vector<uint8_t>> mFilterOutputs; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 177 | vector<unique_ptr<FilterMQ>> mFilterMQs; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 178 | vector<EventFlag*> mFilterEventFlags; |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 179 | vector<DemuxFilterEvent> mFilterEvents; |
| 180 | unique_ptr<FilterMQ> mInputMQ; |
| 181 | unique_ptr<FilterMQ> mOutputMQ; |
| 182 | EventFlag* mInputEventFlag; |
| 183 | EventFlag* mOutputEventFlag; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Demux callbacks used on filter events or IO buffer status |
| 186 | */ |
| 187 | vector<sp<IDemuxCallback>> mDemuxCallbacks; |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 188 | sp<IDemuxCallback> mInputCallback; |
| 189 | sp<IDemuxCallback> mOutputCallback; |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 190 | bool mInputConfigured = false; |
| 191 | bool mOutputConfigured = false; |
| 192 | DemuxInputSettings mInputSettings; |
| 193 | DemuxOutputSettings mOutputSettings; |
| 194 | |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 195 | // Thread handlers |
| 196 | pthread_t mInputThread; |
| 197 | pthread_t mOutputThread; |
| 198 | vector<pthread_t> mFilterThreads; |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 199 | |
| 200 | // FMQ status local records |
| 201 | DemuxInputStatus mIntputStatus; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 202 | /** |
| 203 | * If a specific filter's writing loop is still running |
| 204 | */ |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 205 | vector<bool> mFilterThreadRunning; |
| 206 | bool mInputThreadRunning; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Lock to protect writes to the FMQs |
| 209 | */ |
| 210 | std::mutex mWriteLock; |
| 211 | /** |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 212 | * Lock to protect writes to the filter event |
| 213 | */ |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 214 | // TODO make each filter separate event lock |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 215 | std::mutex mFilterEventLock; |
| 216 | /** |
Amy | 42a5b4b | 2019-10-03 16:49:48 -0700 | [diff] [blame] | 217 | * Lock to protect writes to the input status |
| 218 | */ |
| 219 | std::mutex mInputStatusLock; |
| 220 | /** |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 221 | * How many times a filter should write |
| 222 | * TODO make this dynamic/random/can take as a parameter |
| 223 | */ |
| 224 | const uint16_t SECTION_WRITE_COUNT = 10; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | } // namespace implementation |
| 228 | } // namespace V1_0 |
| 229 | } // namespace tuner |
| 230 | } // namespace tv |
| 231 | } // namespace hardware |
| 232 | } // namespace android |
| 233 | |
| 234 | #endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_ |