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 | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace tv { |
| 28 | namespace tuner { |
| 29 | namespace V1_0 { |
| 30 | namespace implementation { |
| 31 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 32 | using ::android::hardware::EventFlag; |
| 33 | using ::android::hardware::kSynchronizedReadWrite; |
| 34 | using ::android::hardware::MessageQueue; |
| 35 | using ::android::hardware::MQDescriptorSync; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 36 | using ::android::hardware::tv::tuner::V1_0::IDemux; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 37 | using ::android::hardware::tv::tuner::V1_0::IDemuxCallback; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 38 | using ::android::hardware::tv::tuner::V1_0::Result; |
| 39 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 40 | using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; |
| 41 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 42 | class Demux : public IDemux { |
| 43 | public: |
| 44 | Demux(uint32_t demuxId); |
| 45 | |
| 46 | virtual Return<Result> setFrontendDataSource(uint32_t frontendId) override; |
| 47 | |
| 48 | virtual Return<Result> close() override; |
| 49 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 50 | virtual Return<void> addFilter(DemuxFilterType type, uint32_t bufferSize, |
| 51 | const sp<IDemuxCallback>& cb, addFilter_cb _hidl_cb) override; |
| 52 | |
| 53 | virtual Return<void> getFilterQueueDesc(uint32_t filterId, |
| 54 | getFilterQueueDesc_cb _hidl_cb) override; |
| 55 | |
| 56 | virtual Return<Result> configureFilter(uint32_t filterId, |
| 57 | const DemuxFilterSettings& settings) override; |
| 58 | |
| 59 | virtual Return<Result> startFilter(uint32_t filterId) override; |
| 60 | |
| 61 | virtual Return<Result> stopFilter(uint32_t filterId) override; |
| 62 | |
| 63 | virtual Return<Result> flushFilter(uint32_t filterId) override; |
| 64 | |
| 65 | virtual Return<Result> removeFilter(uint32_t filterId) override; |
| 66 | |
| 67 | virtual Return<void> getAvSyncHwId(uint32_t filterId, getAvSyncHwId_cb _hidl_cb) override; |
| 68 | |
| 69 | virtual Return<void> getAvSyncTime(AvSyncHwId avSyncHwId, getAvSyncTime_cb _hidl_cb) override; |
| 70 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 71 | private: |
| 72 | virtual ~Demux(); |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 73 | /** |
| 74 | * To create a FilterMQ with the the next available Filter ID. |
| 75 | * Creating Event Flag at the same time. |
| 76 | * Add the successfully created/saved FilterMQ into the local list. |
| 77 | * |
| 78 | * Return false is any of the above processes fails. |
| 79 | */ |
| 80 | bool createAndSaveMQ(uint32_t bufferSize, uint32_t filterId); |
| 81 | void deleteEventFlag(); |
| 82 | bool writeDataToFilterMQ(const std::vector<uint8_t>& data, uint32_t filterId); |
| 83 | Result startSectionFilterHandler(DemuxFilterEvent event); |
| 84 | Result startPesFilterHandler(DemuxFilterEvent& event); |
| 85 | Result startTsFilterHandler(); |
| 86 | Result startMediaFilterHandler(DemuxFilterEvent& event); |
| 87 | Result startRecordFilterHandler(DemuxFilterEvent& event); |
| 88 | Result startPcrFilterHandler(); |
| 89 | bool writeSectionsAndCreateEvent(DemuxFilterEvent& event, uint32_t sectionNum); |
| 90 | void filterThreadLoop(DemuxFilterEvent* event); |
| 91 | static void* __threadLoop(void* data); |
| 92 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 93 | uint32_t mDemuxId; |
| 94 | uint32_t mSourceFrontendId; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Record the last used filer id. Initial value is -1. |
| 97 | * Filter Id starts with 0. |
| 98 | */ |
| 99 | uint32_t mLastUsedFilterId = -1; |
| 100 | /** |
| 101 | * A list of created FilterMQ ptrs. |
| 102 | * The array number is the filter ID. |
| 103 | */ |
| 104 | vector<unique_ptr<FilterMQ>> mFilterMQs; |
| 105 | vector<DemuxFilterType> mFilterTypes; |
| 106 | vector<EventFlag*> mFilterEventFlags; |
| 107 | /** |
| 108 | * Demux callbacks used on filter events or IO buffer status |
| 109 | */ |
| 110 | vector<sp<IDemuxCallback>> mDemuxCallbacks; |
| 111 | /** |
| 112 | * How many times a specific filter has written since started |
| 113 | */ |
| 114 | vector<uint16_t> mFilterWriteCount; |
| 115 | pthread_t mThreadId = 0; |
| 116 | /** |
| 117 | * If a specific filter's writing loop is still running |
| 118 | */ |
| 119 | vector<bool> mThreadRunning; |
| 120 | /** |
| 121 | * Lock to protect writes to the FMQs |
| 122 | */ |
| 123 | std::mutex mWriteLock; |
| 124 | /** |
| 125 | * How many times a filter should write |
| 126 | * TODO make this dynamic/random/can take as a parameter |
| 127 | */ |
| 128 | const uint16_t SECTION_WRITE_COUNT = 10; |
| 129 | // A struct that passes the arguments to a newly created filter thread |
| 130 | struct ThreadArgs { |
| 131 | Demux* user; |
| 132 | DemuxFilterEvent* event; |
| 133 | }; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace implementation |
| 137 | } // namespace V1_0 |
| 138 | } // namespace tuner |
| 139 | } // namespace tv |
| 140 | } // namespace hardware |
| 141 | } // namespace android |
| 142 | |
| 143 | #endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_ |