blob: 2fdde8dcf8c7956199202ceb77052988305b8f5f [file] [log] [blame]
Amyfd4243a2019-08-16 16:01:27 -07001/*
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>
Amya609d5a2019-08-23 14:38:31 -070021#include <fmq/MessageQueue.h>
Amya4885292019-09-06 10:30:53 -070022#include <set>
Amyfd4243a2019-08-16 16:01:27 -070023
24using namespace std;
25
26namespace android {
27namespace hardware {
28namespace tv {
29namespace tuner {
30namespace V1_0 {
31namespace implementation {
32
Amya609d5a2019-08-23 14:38:31 -070033using ::android::hardware::EventFlag;
34using ::android::hardware::kSynchronizedReadWrite;
35using ::android::hardware::MessageQueue;
36using ::android::hardware::MQDescriptorSync;
Amyfd4243a2019-08-16 16:01:27 -070037using ::android::hardware::tv::tuner::V1_0::IDemux;
Amya609d5a2019-08-23 14:38:31 -070038using ::android::hardware::tv::tuner::V1_0::IDemuxCallback;
Amyfd4243a2019-08-16 16:01:27 -070039using ::android::hardware::tv::tuner::V1_0::Result;
40
Amya609d5a2019-08-23 14:38:31 -070041using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
42
Amyfd4243a2019-08-16 16:01:27 -070043class Demux : public IDemux {
44 public:
45 Demux(uint32_t demuxId);
46
Amya4885292019-09-06 10:30:53 -070047 ~Demux();
48
Amyfd4243a2019-08-16 16:01:27 -070049 virtual Return<Result> setFrontendDataSource(uint32_t frontendId) override;
50
51 virtual Return<Result> close() override;
52
Amya609d5a2019-08-23 14:38:31 -070053 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
Amya4885292019-09-06 10:30:53 -070074 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
94 virtual Return<Result> attachOutputTsFilter(uint32_t filterId) override;
95
96 virtual Return<Result> detachOutputTsFilter(uint32_t filterId) override;
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
Amyfd4243a2019-08-16 16:01:27 -0700106 private:
Amya4885292019-09-06 10:30:53 -0700107 // 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 */
118 Result startSectionFilterHandler(uint32_t filterId, vector<uint8_t> data);
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
Amya609d5a2019-08-23 14:38:31 -0700126 /**
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 */
Amya4885292019-09-06 10:30:53 -0700133 bool createFilterMQ(uint32_t bufferSize, uint32_t filterId);
134 bool createMQ(FilterMQ* queue, EventFlag* eventFlag, uint32_t bufferSize);
Amya609d5a2019-08-23 14:38:31 -0700135 void deleteEventFlag();
136 bool writeDataToFilterMQ(const std::vector<uint8_t>& data, uint32_t filterId);
Amya4885292019-09-06 10:30:53 -0700137 bool readDataFromMQ();
138 bool writeSectionsAndCreateEvent(uint32_t filterId, vector<uint8_t> data);
139 /**
140 * A dispatcher to read and dispatch input data to all the started filters.
141 * Each filter handler handles the data filtering/output writing/filterEvent updating.
142 */
143 bool filterAndOutputData();
144 static void* __threadLoopFilter(void* data);
145 static void* __threadLoopInput(void* user);
146 void filterThreadLoop(uint32_t filterId);
147 void inputThreadLoop();
Amya609d5a2019-08-23 14:38:31 -0700148
Amyfd4243a2019-08-16 16:01:27 -0700149 uint32_t mDemuxId;
150 uint32_t mSourceFrontendId;
Amya609d5a2019-08-23 14:38:31 -0700151 /**
Amya4885292019-09-06 10:30:53 -0700152 * Record the last used filter id. Initial value is -1.
Amya609d5a2019-08-23 14:38:31 -0700153 * Filter Id starts with 0.
154 */
155 uint32_t mLastUsedFilterId = -1;
156 /**
Amya4885292019-09-06 10:30:53 -0700157 * Record all the used filter Ids.
158 * Any removed filter id should be removed from this set.
159 */
160 set<uint32_t> mUsedFilterIds;
161 /**
162 * Record all the unused filter Ids within mLastUsedFilterId.
163 * Removed filter Id should be added into this set.
164 * When this set is not empty, ids here should be allocated first
165 * and added into usedFilterIds.
166 */
167 set<uint32_t> mUnusedFilterIds;
168 /**
Amya609d5a2019-08-23 14:38:31 -0700169 * A list of created FilterMQ ptrs.
170 * The array number is the filter ID.
171 */
172 vector<unique_ptr<FilterMQ>> mFilterMQs;
Amya609d5a2019-08-23 14:38:31 -0700173 vector<EventFlag*> mFilterEventFlags;
Amya4885292019-09-06 10:30:53 -0700174 vector<DemuxFilterEvent> mFilterEvents;
175 unique_ptr<FilterMQ> mInputMQ;
176 unique_ptr<FilterMQ> mOutputMQ;
177 EventFlag* mInputEventFlag;
178 EventFlag* mOutputEventFlag;
Amya609d5a2019-08-23 14:38:31 -0700179 /**
180 * Demux callbacks used on filter events or IO buffer status
181 */
182 vector<sp<IDemuxCallback>> mDemuxCallbacks;
Amya4885292019-09-06 10:30:53 -0700183 sp<IDemuxCallback> mInputCallback;
184 sp<IDemuxCallback> mOutputCallback;
185 // Thread handlers
186 pthread_t mInputThread;
187 pthread_t mOutputThread;
188 vector<pthread_t> mFilterThreads;
Amya609d5a2019-08-23 14:38:31 -0700189 /**
190 * If a specific filter's writing loop is still running
191 */
Amya4885292019-09-06 10:30:53 -0700192 vector<bool> mFilterThreadRunning;
193 bool mInputThreadRunning;
Amya609d5a2019-08-23 14:38:31 -0700194 /**
195 * Lock to protect writes to the FMQs
196 */
197 std::mutex mWriteLock;
198 /**
Amya4885292019-09-06 10:30:53 -0700199 * Lock to protect writes to the filter event
200 */
201 std::mutex mFilterEventLock;
202 /**
Amya609d5a2019-08-23 14:38:31 -0700203 * How many times a filter should write
204 * TODO make this dynamic/random/can take as a parameter
205 */
206 const uint16_t SECTION_WRITE_COUNT = 10;
Amyfd4243a2019-08-16 16:01:27 -0700207};
208
209} // namespace implementation
210} // namespace V1_0
211} // namespace tuner
212} // namespace tv
213} // namespace hardware
214} // namespace android
215
216#endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_