blob: 7f0b0a7d6274829d10f7217df078ad5331c91326 [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:
Hongguang Chenff2c6b02021-08-07 00:12:26 +000056 Demux(int32_t demuxId, std::shared_ptr<Tuner> tuner);
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
101 private:
102 // Tuner service
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000103 std::shared_ptr<Tuner> mTuner;
Hongguang4092f2f2021-07-08 18:49:12 -0700104
105 // Frontend source
106 std::shared_ptr<Frontend> mFrontend;
107
108 // A struct that passes the arguments to a newly created filter thread
109 struct ThreadArgs {
110 Demux* user;
111 int64_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 int32_t mDemuxId = -1;
128 int32_t mCiCamId;
129 set<int64_t> mPcrFilterIds;
130 /**
131 * Record the last used filter id. Initial value is -1.
132 * Filter Id starts with 0.
133 */
134 int64_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<int64_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<int64_t> mRecordFilterIds;
145 /**
146 * A list of created Filter sp.
147 * The array number is the filter ID.
148 */
149 std::map<int64_t, std::shared_ptr<Filter>> mFilters;
150
151 /**
152 * Local reference to the opened Timer Filter instance.
153 */
154 std::shared_ptr<TimeFilter> mTimeFilter;
155
156 /**
157 * Local reference to the opened DVR object.
158 */
159 std::shared_ptr<Dvr> mDvrPlayback;
160 std::shared_ptr<Dvr> mDvrRecord;
161
162 // Thread handlers
Hongguang901aa7b2021-08-26 12:20:56 -0700163 std::thread mFrontendInputThread;
164
Hongguang4092f2f2021-07-08 18:49:12 -0700165 /**
166 * If a specific filter's writing loop is still running
167 */
Hongguang901aa7b2021-08-26 12:20:56 -0700168 std::atomic<bool> mFrontendInputThreadRunning;
169 std::atomic<bool> mKeepFetchingDataFromFrontend;
170
Hongguang4092f2f2021-07-08 18:49:12 -0700171 /**
172 * If the dvr recording is running.
173 */
174 bool mIsRecording = false;
175 /**
176 * Lock to protect writes to the FMQs
177 */
178 std::mutex mWriteLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700179
180 // temp handle single PES filter
181 // TODO handle mulptiple Pes filters
182 int mPesSizeLeft = 0;
183 vector<uint8_t> mPesOutput;
184
185 const bool DEBUG_DEMUX = false;
186};
187
188} // namespace tuner
189} // namespace tv
190} // namespace hardware
191} // namespace android
192} // namespace aidl