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 #pragma once 18 19 #include <aidl/android/hardware/tv/tuner/BnDemux.h> 20 21 #include <fmq/AidlMessageQueue.h> 22 #include <math.h> 23 #include <atomic> 24 #include <set> 25 #include <thread> 26 27 #include "Dvr.h" 28 #include "Filter.h" 29 #include "Frontend.h" 30 #include "TimeFilter.h" 31 #include "Tuner.h" 32 33 using namespace std; 34 35 namespace aidl { 36 namespace android { 37 namespace hardware { 38 namespace tv { 39 namespace tuner { 40 41 using ::aidl::android::hardware::common::fmq::MQDescriptor; 42 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 43 using ::android::AidlMessageQueue; 44 using ::android::hardware::EventFlag; 45 46 using FilterMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>; 47 48 class Dvr; 49 class Filter; 50 class Frontend; 51 class TimeFilter; 52 class Tuner; 53 54 class Demux : public BnDemux { 55 public: 56 Demux(int32_t demuxId, uint32_t filterTypes); 57 ~Demux(); 58 59 ::ndk::ScopedAStatus setFrontendDataSource(int32_t in_frontendId) override; 60 ::ndk::ScopedAStatus openFilter(const DemuxFilterType& in_type, int32_t in_bufferSize, 61 const std::shared_ptr<IFilterCallback>& in_cb, 62 std::shared_ptr<IFilter>* _aidl_return) override; 63 ::ndk::ScopedAStatus openTimeFilter(std::shared_ptr<ITimeFilter>* _aidl_return) override; 64 ::ndk::ScopedAStatus getAvSyncHwId(const std::shared_ptr<IFilter>& in_filter, 65 int32_t* _aidl_return) override; 66 ::ndk::ScopedAStatus getAvSyncTime(int32_t in_avSyncHwId, int64_t* _aidl_return) override; 67 ::ndk::ScopedAStatus close() override; 68 ::ndk::ScopedAStatus openDvr(DvrType in_type, int32_t in_bufferSize, 69 const std::shared_ptr<IDvrCallback>& in_cb, 70 std::shared_ptr<IDvr>* _aidl_return) override; 71 ::ndk::ScopedAStatus connectCiCam(int32_t in_ciCamId) override; 72 ::ndk::ScopedAStatus disconnectCiCam() override; 73 74 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; 75 76 // Functions interacts with Tuner Service 77 void stopFrontendInput(); 78 ::ndk::ScopedAStatus removeFilter(int64_t filterId); 79 bool attachRecordFilter(int64_t filterId); 80 bool detachRecordFilter(int64_t filterId); 81 ::ndk::ScopedAStatus startFilterHandler(int64_t filterId); 82 void updateFilterOutput(int64_t filterId, vector<int8_t> data); 83 void updateMediaFilterOutput(int64_t filterId, vector<int8_t> data, uint64_t pts); 84 uint16_t getFilterTpid(int64_t filterId); 85 void setIsRecording(bool isRecording); 86 bool isRecording(); 87 void startFrontendInputLoop(); 88 89 /** 90 * A dispatcher to read and dispatch input data to all the started filters. 91 * Each filter handler handles the data filtering/output writing/filterEvent updating. 92 * Note that recording filters are not included. 93 */ 94 bool startBroadcastFilterDispatcher(); 95 void startBroadcastTsFilter(vector<int8_t> data); 96 97 void sendFrontendInputToRecord(vector<int8_t> data); 98 void sendFrontendInputToRecord(vector<int8_t> data, uint16_t pid, uint64_t pts); 99 bool startRecordFilterDispatcher(); 100 101 void getDemuxInfo(DemuxInfo* demuxInfo); 102 int32_t getDemuxId(); 103 bool isInUse(); 104 void setInUse(bool inUse); 105 void setTunerService(std::shared_ptr<Tuner> tuner); 106 107 private: 108 // Tuner service 109 std::shared_ptr<Tuner> mTuner; 110 111 // Frontend source 112 std::shared_ptr<Frontend> mFrontend; 113 114 // A struct that passes the arguments to a newly created filter thread 115 struct ThreadArgs { 116 Demux* user; 117 int64_t filterId; 118 }; 119 120 static void* __threadLoopFrontend(void* user); 121 void frontendInputThreadLoop(); 122 123 /** 124 * To create a FilterMQ with the next available Filter ID. 125 * Creating Event Flag at the same time. 126 * Add the successfully created/saved FilterMQ into the local list. 127 * 128 * Return false is any of the above processes fails. 129 */ 130 void deleteEventFlag(); 131 bool readDataFromMQ(); 132 133 int32_t mDemuxId = -1; 134 int32_t mCiCamId; 135 set<int64_t> mPcrFilterIds; 136 /** 137 * Record the last used filter id. Initial value is -1. 138 * Filter Id starts with 0. 139 */ 140 int64_t mLastUsedFilterId = -1; 141 /** 142 * Record all the used playback filter Ids. 143 * Any removed filter id should be removed from this set. 144 */ 145 set<int64_t> mPlaybackFilterIds; 146 /** 147 * Record all the attached record filter Ids. 148 * Any removed filter id should be removed from this set. 149 */ 150 set<int64_t> mRecordFilterIds; 151 /** 152 * A list of created Filter sp. 153 * The array number is the filter ID. 154 */ 155 std::map<int64_t, std::shared_ptr<Filter>> mFilters; 156 157 /** 158 * Local reference to the opened Timer Filter instance. 159 */ 160 std::shared_ptr<TimeFilter> mTimeFilter; 161 162 /** 163 * Local reference to the opened DVR object. 164 */ 165 std::shared_ptr<Dvr> mDvrPlayback; 166 std::shared_ptr<Dvr> mDvrRecord; 167 168 // Thread handlers 169 std::thread mFrontendInputThread; 170 171 /** 172 * If a specific filter's writing loop is still running 173 */ 174 std::atomic<bool> mFrontendInputThreadRunning; 175 std::atomic<bool> mKeepFetchingDataFromFrontend; 176 177 /** 178 * If the dvr recording is running. 179 */ 180 bool mIsRecording = false; 181 /** 182 * Lock to protect writes to the FMQs 183 */ 184 std::mutex mWriteLock; 185 186 // temp handle single PES filter 187 // TODO handle mulptiple Pes filters 188 int mPesSizeLeft = 0; 189 vector<uint8_t> mPesOutput; 190 191 const bool DEBUG_DEMUX = false; 192 193 int32_t mFilterTypes; 194 bool mInUse = false; 195 }; 196 197 } // namespace tuner 198 } // namespace tv 199 } // namespace hardware 200 } // namespace android 201 } // namespace aidl 202