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