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