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