blob: 5212eae02d736c1206cd94d2bc5b400678bdf031 [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
Amy Zhang585bb5e2020-09-15 22:52:38 +000051class Demux : public IDemux {
Amy Zhangbb94eeb2020-07-09 22:48:04 -070052 public:
53 Demux(uint32_t demuxId, sp<Tuner> tuner);
54
55 ~Demux();
56
Amy Zhangbb94eeb2020-07-09 22:48:04 -070057 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);
Amy Zhang68afca62020-07-20 18:28:58 -070099 void sendFrontendInputToRecord(vector<uint8_t> data, uint16_t pid, uint64_t pts);
Amy Zhangbb94eeb2020-07-09 22:48:04 -0700100 bool startRecordFilterDispatcher();
101
102 private:
103 // Tuner service
104 sp<Tuner> mTunerService;
105
106 // Frontend source
107 sp<Frontend> mFrontend;
108
109 // A struct that passes the arguments to a newly created filter thread
110 struct ThreadArgs {
111 Demux* user;
112 uint64_t filterId;
113 };
114
115 static void* __threadLoopFrontend(void* user);
116 void frontendInputThreadLoop();
117
118 /**
119 * To create a FilterMQ with the next available Filter ID.
120 * Creating Event Flag at the same time.
121 * Add the successfully created/saved FilterMQ into the local list.
122 *
123 * Return false is any of the above processes fails.
124 */
125 void deleteEventFlag();
126 bool readDataFromMQ();
127
128 uint32_t mDemuxId = -1;
129 uint32_t mCiCamId;
130 set<uint64_t> mPcrFilterIds;
131 /**
132 * Record the last used filter id. Initial value is -1.
133 * Filter Id starts with 0.
134 */
135 uint64_t mLastUsedFilterId = -1;
136 /**
137 * Record all the used playback filter Ids.
138 * Any removed filter id should be removed from this set.
139 */
140 set<uint64_t> mPlaybackFilterIds;
141 /**
142 * Record all the attached record filter Ids.
143 * Any removed filter id should be removed from this set.
144 */
145 set<uint64_t> mRecordFilterIds;
146 /**
147 * A list of created Filter sp.
148 * The array number is the filter ID.
149 */
150 std::map<uint64_t, sp<Filter>> mFilters;
151
152 /**
153 * Local reference to the opened Timer Filter instance.
154 */
155 sp<TimeFilter> mTimeFilter;
156
157 /**
158 * Local reference to the opened DVR object.
159 */
160 sp<Dvr> mDvrPlayback;
161 sp<Dvr> mDvrRecord;
162
163 // Thread handlers
164 pthread_t mFrontendInputThread;
165 /**
166 * If a specific filter's writing loop is still running
167 */
168 bool mFrontendInputThreadRunning;
169 bool mKeepFetchingDataFromFrontend;
170 /**
171 * If the dvr recording is running.
172 */
173 bool mIsRecording = false;
174 /**
175 * Lock to protect writes to the FMQs
176 */
177 std::mutex mWriteLock;
178 /**
179 * Lock to protect writes to the input status
180 */
181 std::mutex mFrontendInputThreadLock;
182
183 // temp handle single PES filter
184 // TODO handle mulptiple Pes filters
185 int mPesSizeLeft = 0;
186 vector<uint8_t> mPesOutput;
187
188 const bool DEBUG_DEMUX = false;
189};
190
191} // namespace implementation
192} // namespace V1_0
193} // namespace tuner
194} // namespace tv
195} // namespace hardware
196} // namespace android
197
198#endif // ANDROID_HARDWARE_TV_TUNER_V1_1_DEMUX_H_