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